My question is regarding the memory management of a 4GB addressable system
char *p = NULL;
Does it occupy any memory?
If so, where in the heap or stack and how much?
Also tell about char **p=NULL;
My question is regarding the memory management of a 4GB addressable system char *p
Share
On a typical 32 bit system, it takes 4 bytes.
Assuming your examples are a definition of a local variable in some function, those bytes are taken from the stack. Although:
If, instead, it’s a global variable (or, in general, a variable with static storage duration), on most systems there’s a special region of memory (separated from the stack and the so-called heap) used for them. Often it’s just a region of memory mapped directly from the executable image in copy-on-write mode. So here the 4 bytes are occupied both in this particular memory area, both in space inside the executable.
The same holds for
char **p, which, in line of principle, has no reason for being bigger or different somehow fromchar *.By the way, if
char * porchar ** pwere part of an aggregate data type (typically astruct), the space they take comes from wherever thestructis allocated – if thestructvariable is a local variable, it comes from the stack, if it’s allocated dynamically withmallocon the heap, if it’s a global it comes from the special memory region for globals. Keep in mind that, talking about space taken bystructs, additional considerations about padding come into play.Notice that all those are considerations valid for “typical” 32-bit systems; nothing stops some bizarre architecture from making
char **different in size fromchar *(although I don’t see any reason to do that). Still, you can perform a direct check by using thesizeofoperator.As far as the standard is concerned, I think that the only constraint imposed to pointer size is that any pointer to data can be converted to and from
void *without loss of information (actually, the standard doesn’t ever mention the stack or registers). Also, keep in mind that the compiler is allowed to do anything it wants as far as the “observable behavior” is coherent with what is requested from the standard, so there’s no real guarantee on these implementation details mandated by the standard, although more details may be found in the documentation of the compiler you are using.