I'm trying to figure out the C
memory layout of global variables.
Here is the program I used:
#include <stdio.h>
int a = 1;
char *b = "moish";
//char b[6] = {'m','o','i','s','h',0};
int c = 3;
int d = 4;
int e = 5;
int main(int argc, char **argv)
{
printf("location: %p\n",&a);
printf("location: %p\n", b);
printf("location: %p\n",&c);
printf("location: %p\n",&d);
printf("location: %p\n",&e);
return 0;
}
When I compiled and ran it I got what I expected, assuming a reasonable alignment
in which variable c
is located at 0x56294fab001c
instead of 0x56294fab001a
etc.
location: 0x56294fab0010
location: 0x56294fab0014
location: 0x56294fab001c
location: 0x56294fab0020
location: 0x56294fab0024
When I use char *b = "moish"
I get a totally different address:
location: 0x55c676551010
location: 0x55c676350784
location: 0x55c676551014
location: 0x55c676551018
location: 0x55c67655101c
Why is that? I thought the 2 options are equivalent, no?