What I really wanted to know was where it is stored . But you guys
tell me that it is implementation specific . Actually this was asked
in a job interview . And as far as I know the interviewer did not
mention any platform .
Ah, I usually ask this question in job interviews. I have also been
asked many times in when I was looking for a job.
In my opinion, it is important for a systems software engineer (who
works on embedded or device driver software) to know the "general"
memory structure of a process image.
In general, it is safe to say the following since it is similar on
Unix, Linux, Windows, ...
char *a; // uninitialized data segment
char *b = "hello"; // b is in uninitialized data segment, b's value
"hello" is in constant data segment
static char c; // file scope, not visible from other files
char d[8] = "world"; // initialized data segment, array of 8 bytes,
initialized first 6
// char d[800]; will cause file size of executable to be increased
char e[8]; // uninitialized data segment
// char e[800]; will cause process to require more memory, but same
executable file size
// foo is in the code segment
void foo(char f) // f is copied onto stack
{
char g; // allocated from stack
static char h; // data segment, not on stack so the value is
"static"
char *i = malloc(8); // heap, potential memory leak
// It is also important to know the difference of b and d
d[0] = 'h'; // OK
b[0] = 'y'; // likely to get a segmentation fault
}