bss(uninitialized data) segment allocation

Hi
1) Please go through the following code :

     char string2[12];
     char string1[6];
     main\(\)
     \{
        memcpy\(string2,"SENDER      ",12\);
        strcpy\(string1,"******"\);
        printf\("%s\\n%s\\n",string1,string2\);
     \}

2) and the output of size command for the executable is :

   1904 \+ 415 \+ 18 = 2337

3) and the output is as expected ( gdb output ):

  Breakpoint 1, main \(\) at test.c:5
  5         memcpy\(string2,"SENDER      ",12\);
  2: string2 = '\\000' <repeats 11 times>
  1: string1 = "\\000\\000\\000\\000\\000"
  \(gdb\) s
  6         strcpy\(string1,"******"\);
  2: string2 = "SENDER      "
  1: string1 = "\\000\\000\\000\\000\\000"
  \(gdb\) s
  7         printf\("%s\\n%s\\n",string1,string2\);
  2: string2 = "\\000ENDER      "
  1: string1 = "******"

My question is in what order the global variables are stored in the memory.

For the above example, from the output of size command 18 is the total size of the global variables and when the strcpy statement is executed, it is overwriting the first character of string2 with NULL in order to terminate string1 with NULL.

So, how the variables got stored in memory?
1) Is string1 followed by string2?
2) Is string2 followed by string1?

Could anybody help in this regard
Thanks

just insert the following statement in your code:

fprintf(stdout,"string1: %d, string2: %d", string1,string2);

This should print the starting address of the two strings. Run the program several times and see if you can get any pattern (of memory allocation) from that.

Cheers!

Just tried this on a FreeBSD system. The addresses allocated are the same every time, as no other memory is allocated in the interval between program runs.
Also, the allocation is in the order that is specified in the program. i.e. string2 is allocated a starting address that is (12 bytes) before that of string1.
Why exactly are you interested in knowing the order of allocation of the variables?

If string2 were

char string2[13];

then the starting address of string1 may change, depending on your system's architecture. You cannot depend on the starting position of objects in memory.

This is because the compiler may adjust the starting byte of string1 to match the addressing scheme your system uses. Or to optimize access to the object.

Why it is eating the first character of string2 only, if string2 is reserved first 12 bytes or whatever and string1 next 6 bytes

The order of declaration is:
char string2[12];
char string1[6];

The statement order is :
memcpy(string2,"SENDER ",12);
strcpy(string1,******);

It is interesting!!!

I got the answer by analyzing all the replies from you.

I used the fprintf statement to know the starting address of the strings.
On my m/c, I found that it is storing string1 first and then string 2.

So, after executing strcpy(string1,"******") statement it is rewriting the first character of string2 with NULL in order to terminate string1 as strcpy does.

Is the order of storing is OS dependent or compiler dependent ?

Thanks a lot.

It would depend on the OS, the compiler and the architecture of your system.. I think.

You can use nm on the compiled file (using -g option). This will give you virtual addresses of objects and functions in your compiled code.

IF you compile

gcc -S myprog.c

The compiler will create assembly language output -> myprog.s You can see the relative location of objects by editing the file.