0

I'm compiling the following snippet into assembly

void function(int a, int b, int c) {
   char buffer1[5];
   char buffer2[10];
}

void main() {
  function(1,2,3);
}

This is the assembly snippet of function

function:
    pushq   %rbp
    movq    %rsp, %rbp
    subq    $48, %rsp ; why is 48 being subtracted from the stack pointer?
    movl    %edi, -36(%rbp)
    movl    %esi, -40(%rbp)
    movl    %edx, -44(%rbp)
    movq    %fs:40, %rax
    movq    %rax, -8(%rbp)
    xorl    %eax, %eax

I'm running this on a 64-bit machine, so I thought three 8-byte words was enough to hold both buffer1 and buffer2.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
rgarci0959
  • 539
  • 1
  • 4
  • 8
  • Alignment might be an issue. – Some programmer dude Feb 11 '18 at 21:27
  • 1
    Possible duplicate of [What is the direction of stack growth in most modern systems?](https://stackoverflow.com/q/664744/608639) and [Why do stacks typically grow downwards?](https://stackoverflow.com/q/2035568/608639) Related, see [What is x86_64 red zone](https://www.google.com/search?q=what+is+x86_64+red+zone). You should reconsider accepting the answer below. I think it could be improved, and by marking as accepted you signal you do not want more [possibly better] answers. – jww Feb 12 '18 at 03:21
  • Above jww assumes that your qustion is "Why down,not up?" An alternative quesiton could be "Why change, not keep fixed?" The fact that you talk about the size points at another, but unclear question. Please explain what your question is. Maybe by describing what alternative you were expecting. – Yunnosch Sep 11 '18 at 04:27

1 Answers1

-1

At first glance this appears to be an issue of memory alignment. This question goes into more details, and I would take special note of joshperry's answer: https://stackoverflow.com/a/381368

Wikipedia also talks about this here: Data structure alignment

In short, char buffer1[5] will create two 64-byte words (chars 1-4 and 5+). If it didn't, then every time you referred to buffer2, the system would have to perform at least two operations to get the correct starting point for buffer2.

cegfault
  • 6,442
  • 3
  • 27
  • 49
  • 1
    An array of five bytes will need five bytes. It might be placed on a nice 64-bit boundary, and nothing will most likely be placed in the three bytes after it (to make it an even 8 bytes for the next array), but it will not split the elements of the array like that. Unless you use a system where `char` is 16 bits? – Some programmer dude Feb 11 '18 at 21:32
  • a `char` is one byte by C definition, but a `char` **pointer** is not. A char array is a char pointer, which is 64bits on a 64bit CPU. – cegfault Feb 11 '18 at 21:36
  • To be clear, I'm not *positive* my answer is correct, but *at first glance* I would assume this to be a memory alignment issue. Compilers handle 5-char arrays differently than five separate chars – cegfault Feb 11 '18 at 21:37
  • Ah, no an array is *not* a pointer. It can *decay* to a pointer to its first element, but the array itself is a single contiguous object. – Some programmer dude Feb 11 '18 at 21:39
  • My understanding was that a pointer to a 5-char block of memory was roughly equivalent to a 5-char array. A pointer just points to a location in memory. An array declares a block of memory and points to it. Again **I'm not certain**, so I fully admit it's possible I'm wrong or misunderstanding something. – cegfault Feb 11 '18 at 21:40
  • P.S. IIRC, C and C++/C# handle arrays/pointers differently, and I *thought* that gcc (for one compiler) treated arrays and pointers similarly, but it's been too many years since I studied it to be certain anymore..... – cegfault Feb 11 '18 at 21:41