1

I was just looking into the memory allocation of a program in C. I know that all the global and static variables are stored in a heap. Also, the stack stores all the function calls. I do have one doubt here though. Say I am calling the following function:

int ret;
int num = 10;
int arr[3] = {1,2,3};
int *ptr = &arr[0];
ret = giveNumber(num, ptr);

Here, I read that the parameters of the function call giveNumer() would also be stored in the same stack. But in what order will they be stored? If I popped the top of stack, which parameter will be popped first, num or ptr?

1
  • Depends on your architecture and has nothing to do with your debugger. Commented Apr 9, 2012 at 17:12

2 Answers 2

3

I know that all the global and static variables are stored in a heap.
No, thats not true.
As per the standard they are stored in implementation defined memory regions, typically the Data segment and the BSS.

If I popped the top of stack, which parameter will be popped first, num or ptr
The order of evaluation of arguments to a function is Unspecified.
So it depends on your compiler implementation. An compiler might evaluate the arguments from:

  • Left to Right or
  • Right to Left or
  • Any other random order

So the behavior & order you see would depend on this.

Sign up to request clarification or add additional context in comments.

Comments

0

Adding to what @Als has already mentioned, most of the compilers on x86 follow _cdecl calling convention where arguments are evaluated from right to left. Learn more here

http://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions#Standard_C_Calling_Conventions

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.