Search code examples
cpointersmemorydynamic-memory-allocation

Is there a difference between the locations assigned to static and dynamic variable?


Is there a limitation to the locations dynamic variables can have, and is it different for static variables? Is there a partition in the memory of the process where dynamic variables can't be allocated and static variables can be allocated?


I was working with an online IDE for C and experimenting with pointers when I noticed dynamically allocated variables appeared to have a shorter direction when printing them. (Example)

  • Static variable pointer: 0x7ffc49f6fbe4
  • Dynamic variable pointer: 0x15512a0

At first, this made me think that the pointers of dynamic variables had a smaller size than the ones of static variables. I tried a few things to know if this was the case: first I tried looking at the pointers of various types to know if this only applied to dynamic variables. Then, I tried sizeof() with dynamic and static variable pointers:

  int a;
  int *b=malloc(sizeof(int));
  printf("%lu\n%lu",sizeof(&a),sizeof(b));

Both returned 8 bytes. I counted the bytes in the printed hexadecimal values. Static variables had 16 hex digits = 8 bytes, while the dynamic variables had 7 hex digits = 4 bytes. This halving of the byte gave me another idea: maybe static and dynamic directions have different sections of the memory where they can be allocated, so when I print the direction of a dynamic pointer it doesn't bother to give me the whole direction and just prints the sections a dynamic pointer can occupy. This was reinforced when I added 1 to the directions and the last hex was the one to change in both pointers. If I added enough to force the print to show another digit it also showed the new one:

  int a, *b;
  int *c=malloc(sizeof(int));
  printf("%p\n%p\n%p",&b,c,c+67108864);

Shows:

0x7fffc15b5920
0x211e2a0
0x1211e2a0

I tried looking for this question but couldn't find it, so I am making it.


Solution

  • There is nothing particularly special about the locations of various objects. Where they are is simply a matter of the need to organize memory to support various features. In the course of doing that, arbitrary choices are made, and other choices could be possible.

    There is various software involving in planning the layout of memory for a program and loading it into memory. Static objects are defined in the executable file for a program, having been put there during the compiler and linking process. The program loader reads the executable file to see how much memory it needs for those objects and plans some space for them in the virtual address space of the process being created.

    Processes also have a stack, so the program loader plans some space for that. Typically, the executable file contains information on how much stack space the program wants.

    When the program starts, software within in initializes the C environment, including planning space for dynamic allocations.

    There are complications to this, such as needing to load additional static objects when a program calls routines in a dynamically loaded library, reserving additional space for stacks of new threads within the process. Essentially, the people who write the software that loads programs and the software that manages memory within a process have developed plans for this, so, on any particular operating system, the default layout has static objects in some portion of the address space, stack in another portion, and dynamically allocated memory in another portion.

    … another idea: maybe … when I print the direction of a dynamic pointer it doesn't bother to give me the whole direction and just prints the sections a dynamic pointer can occupy.

    When you print a pointer with %p, complete information about it is printed. (Note that a pointer should be converted to (void *) when printing this way.) It is not just a portion of the address information. If the %p output for some pointers have more or fewer digits than others, that is just because the number of digits needed to display their values is more or fewer, the same as it is with numbers generally.