Difference between variable addresses
Why do the address variables differ by a certain amount every time I run the program (as in "printf ("% d% d \ n ", & a, & b);". It will print "1000 988" in one pass ", "924 912" elsewhere, "1288 1276", etc., etc.)? Does the compiler take up a given amount of memory after each variable declaration where nothing can be written? If so, what does it depend on? some variables in my program, the smallest difference between them was 12 bytes, and it reached 212. This was the only case where the difference was not a multiple of twelve (in other cases it was 24, 36 or 48 bytes) Is there any reason for this? Since my variables were of type int (occupying 4 bytes on my system), can the difference between my variable addresses be less than 12 (e.g.4)? Are these addresses distinguishable depend on the types of variables? If so, how? Thank you in advance!
source to share
Most operating systems today use address space allocation randomization to make it difficult to record certain types of malware. (A type that writes code to memory and then tries to force the program to transfer control to it, now you have to guess what address the program should get to jump.) As a result, variables will not have the same address every time you run the program.
Depending on the type of the variable, how it is allocated, and what OS and your architecture is running, the size and alignment of the variables will vary. The compiler and runtime may or may not always fit within a four-, eight-, or sixteen-byte boundary. For example, the x86_64 ABI function call always starts the function stack frame on a sixteen-byte boundary, and some implementations malloc()
always return an address that is divisible by sixteen because this is necessary to store vectors on some CPUs.
If you want to know what the compiler does, you can try building to build. In gcc or clang, you can do this with a flag -S
.
source to share
If you are asking why the memory address for a variable is different between different executable entries, the ASLR answer that exists to make it difficult to exploit security concerns in your code (see https://en.wikipedia.org/wiki/Address_space_layout_randomization ).
If you disable ASLR, you will get the same address for the given variable every time you run the executable.
See also Difference between gdb addresses and "real" addresses?
source to share
Your linker (and to some extent, your compiler) exposes your application's address space. The linker usually creates a floatable image based on some address (eg zero). Apparently your loader is placing the relocatable image in different places when it starts.
Does the compiler take up a given amount of memory after each variable declaration where nothing can be written?
Generally, UNLESS is not required, the next variable must be aligned. Variables are usually aligned at multiples of the size of the variable.
It looks like your compiler is allocating memory for something that you just don't account for.
source to share