Static and dynamic memory addresses in C

printf("address of literal: %p \n", "abc"); char alpha[] = "abcdef"; printf("address of alpha: %p \n", alpha); 

The above literal is stored in static memory, alpha is stored in dynamic memory. I read in a book that some compilers show these two addresses using a different number of bits (I only tried to use gcc on Linux, and it shows a different number of bits). Does it depend on the compiler, operating system and hardware?

+4
source share
2 answers

I was only trying to use gcc on Linux and it shows a different number of bits

It is not that he "uses a different number of bits." As far as I know, Linux - at least when launching the main platforms that I know (for example, x86, x64, ARM32), has no “near” and “far” pointers. For example, on x86, each pointer has a width of 32 bits and on x64, each pointer has a width of 64 bits.

It's simple...

  • the compiler probably allocates an alpha array on the stack (which it is allowed to do since it has an automatic storage duration). Most likely, this is not stored in "dynamic memory", which is stupid, because it is associated with excessive dynamic allocation, which is one of the slowest things you can do with memory.)
  • Meanwhile, literals themselves, with a static storage duration, are stored elsewhere (usually in the data segment of the executable file);
  • and, in addition, the OS memory manager sometimes places these two things (the stack and the executable image) far from each other, so one of them has addresses that start with a large number of zeros, while the addresses in the other do not have many leading zeros.
  • Also, the default behavior of %p in your libc implementation occurs to not print leading zeros.
+1
source

alpha saved, for example. in a stack or other segment of dynamic memory. literal is stored inside a code segment. These are different ranges of addresses.

Addresses are platform dependent. In most cases, the size of the pointer is 4 bytes, but the addresses for different segments are in different ranges.

Addresses are platform dependent.

The component is responsible for assigning the address. You might want to enable an option that allows the linker to create an address card file.

Dynamic parts are also called data segments. Static parts are code segments. You will find a lot of literature looking for this term for your platform (for example, the search for “x86 memory segmentation”).

0
source

All Articles