Edison
Edison

Reputation: 4281

Contiguous address of variables

Consider the following code

void func(int*list, int length)
   {
    int temp = 55;
    cout<<"&list: "<<&list<<endl;
    cout<<"&length: "<<&length<<endl;
    cout<<"&temp: "<<&temp;
  }

From main i am calling the func and giving it arguments. Why i am not getting adjacent addresses that is all arguments are of same size.Only list and length are 4 bytes apart in addresses and are adjacent.Shouldn't temp be also adjacent.

output is below but it varies.

&list:   00D5F8A0
&length: 00D5F8A4
&temp:   00D5F88C  //why not 00D5F8A8

Upvotes: 0

Views: 158

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320391

Why did you expect them to be adjacent?

The arguments are typically prepared by the calling code. If they are physically passed through stack, they will typically have adjacent addresses.

Local variables, on the other hand, are typically managed internally by the function when the call has already occurred.

Now, between the former and the latter a lot of things can (and will) happen that affect the state of the stack. For example, the call itself will typically store the return address in the stack. The prologue code of the function will typically also store some household data in the stack. All that extra data will typically be wedged between the parameter values and local variables, so they won't typically be immediately adjacent in memory.

And that's just one scenario. The details of the process are very implementation dependent. The parameters might be passed through registers and then (if necessary) copied to stack memory by the function itself. But even in such cases it is not guaranteed that parameters and local variables will be stored contiguously.

Upvotes: 1

Kevin
Kevin

Reputation: 2182

These addresses are all in stack memory. There are probably other values that your program is storing between the parameter and the local variables. The layout of a stack-frame varies by compiler and operating system, but a common layout might look like this:

TOP OF STACK FRAME
==================
| Function Parameters
==================
| Return Address
==================
| Base Pointer
==================
| Local Variables
==================
BOTTOM OF STACK FRAME

Upvotes: 1

keshlam
keshlam

Reputation: 8058

list and length are part of the stack frame created when the caller set up the arguments to this function. The stack frame also contains a return address, and may contain other information depending on the calling conventions involved.

temp's address relative to the stack pointer, on the other hand, is determined by the called function, and is likely to be just above the stack frame... which, as noted above, may not be adjacent to the arguments.

Of course, if you're coding properly, you shouldn't have to care about that. But it's good to understand when tracking down pointer errors.

Upvotes: 3

Zak
Zak

Reputation: 12613

All of these variables exist in the stack portion of the memory allocated to the process. The parameters given to a function reside in the stack frame, while variables declared in the function exist in the function's stack atop the process's stack.

Upvotes: 0

Related Questions