2

I'm trying to disassemble this code:

int main()
{
    int arr[5] = {1,2,3,4,5};
    int i;
    for(i=0;i<5;i++)
    {
        printf("Element:%d\n",arr[i]);
    }
    return 0;
}

IDA output: sub esp, 30h

Question: What does it mean? I thought it would be 18h(5x4+4)

2
  • Can you follow the assembly to see what it's doing with the extra space? (If not you might want to post some more if it's not too long.) Which compiler or optimisation settings - I assume you do still have a loop, it hasn't unrolled it? Is it using pushes to set up function call arguments, or is it storing them to [esp], [esp+4] etc., i.e. has it reserved some extra stack space for those?
    – Rup
    Commented Jan 9, 2016 at 0:49
  • 4
    sun esp,30h means the compiler made a space in stack for 0x30 the space / value is compiler and optimization dependent and you shouldn't be caring much if the compiler thinks it needs a football ground to play chess then let it play in a soccer ground you concentrate on the moves
    – blabb
    Commented Jan 9, 2016 at 9:48

1 Answer 1

4

It seems like you are compiling under amd64 (which is quite likely in 2016 ;). Compiler allocates all stack memory (but not only, the same for function arguments) with alignment to 8 bits. You assume, that int is 32 bits, and it might me 32 bits on your machine, but compiler still will be allocating 8 bytes (64 bits). So 5*8+8=48 = 0x30.

Try allocating an array of five chars (aka uint8_t). The compiler will still allocate 8 bytes * number of elements.

Read about attribute packed (gcc) or pragma pack (Windows).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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