1

I am following a book where they explain about the buffer overflow. I have a question based on buffer overflow output in C. There are two char array(buffer_one[8] and buffer_two[8]) and one integer (value).

In a x86_64 system, I am declaring the variables in an order.

    int value = 5; 
    char buffer_one[8], buffer_two[8];

However when i dump the address of these three variables, I received them in below order(value at d4, then buffer_one at d8 and then buffer_two at e0).

Buffer_one is at 0x7ffe7860b2d8 and contains one
Buffer_two is at 0x7ffe7860b2e0 and contains two
Value is at 0x7ffe7860b2d4 and contains 5

Same when I tried in a i686 system, I observed different order of memory allocation (buffer_two at 28, then buffer_one at 30 and then value at 38).

Buffer_one is at 0xbfef7330 and contains one
Buffer_two is at 0xbfef7328 and contains two
Value is at 0xbfef7338 and contains 5

MY QUESTIONS:

  1. Why the order of memory allocation differs despite being orderly declared?
  2. If I declare both char array before int, why int variable is always allocated before buffer_two in x86_64 system?
  3. What will be the order in ARM architecture?
1
  • 3
    The compiler is free to order the memory for variables in any way it sees fit, it doesn't have to follow the order you declared them in Commented Dec 2, 2021 at 13:32

1 Answer 1

4

The compiler does what it feels like doing. Or what the developer(s) who write the compiler felt like doing, depending on whether the compiler is sentient or not.

This may include:

  • The compiler allocates memory in the order it reads definitions.
  • The compiler keeps a list of objects that need only one-byte alignment, a list of objects that need two-byte alignment, a list for four-byte alignment, and so on. As it reads each definition, it puts that object on the appropriate list. Later, it allocates memory for the most restrictive list first, starting with an aligned address. Then it allocates memory for the next most restrictive list, and so on. Maybe it processes the items on each list in the order it read them. Maybe it processes them in reverse order, just because of how the list was constructed.
  • As the compiler reads the definitions, it puts the identifiers in a hash table, because that is a good data structure for managing strings. Later, when the compiler is allocating memory, it processes the hash table in its physical order. The result is a mishmash of object ordering with no order apparent to the user.

The C standard does not specify anything about the order in memory of unrelated objects. Compilers do whatever makes sense to their programmers.

3
  • "what the developer(s) who write the compiler felt like doing"? I am using GCC compiler in all the devices and is there any order mentioned in the gcc compiler, if so? Commented Dec 2, 2021 at 13:42
  • 1
    @SanthoshKumar do not assume that gcc will always do the same. How internals are handled changes from version to version. As Eric stated many parameters are taken into account, but the order of definitions in the source file is not one of them - simply because it does not matter Commented Dec 2, 2021 at 14:29
  • 1
    In case anyone has questions regarding the distinctions between memory allocation during compilation, as opposed to memory allocation during run-time, this post explains it.. ( I've had to remind myself of these differences occasionally :) )
    – ryyker
    Commented Dec 2, 2021 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.