Bryan R
Bryan R

Reputation: 111

Order of parameter and variables on stack in C

Several years ago, I put together this little memory address exploration for my students to help them to understand pointers, arrays, the stack, and the heap.

I just compiled and ran it in a new environment (gcc on a AWS Linux server) and the order of the parameters for foo are different from what I would expect. The local function variables (d1 and e1) now have a higher address in comparison to the function parameters (a1, b1, and c1).

The addresses for the parameters / variables in function foo are listing as:

&a1: fa2740fc
&b1: fa2740f0
&c1: fa2740e8
&d1: fa27410c
&e1: fa274100

Any thoughts on why variables d1 and e1 have higher addresses than a1, b1, and c1?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int* foo (int a1, int b1[], int *c1)
{
  int d1 = *c1 + a1;
  int *e1;
  
  e1 = malloc (sizeof(int));

  printf ("5) Addresses of arguments/variables of foo:\n");
  printf ("   Update your stack sketch.\n");
  printf ("     &a1: %x\n", &a1);
  printf ("     &b1: %x\n", &b1);
  printf ("     &c1: %x\n", &c1);
  printf ("     &d1: %x\n", &d1);
  printf ("     &e1: %x\n\n", &e1);

  printf ("6) Values of arguments/variables in foo:\n");
  printf ("   Include these on your stack sketch as well.\n");
  printf ("     a1: %x\n", a1);
  printf ("     b1: %x\n", b1);
  printf ("     c1: %x\n", c1);
  printf ("     d1: %x\n", d1);
  printf ("     e1: %08x\n\n", e1);

  printf ("7) *c1 == %x, why?  Explain using your stack drawing.\n\n", *c1); 
  
  printf ("8) e1 is a reference to an integer, much like c1.  Why is e1 so\n ");
  printf ("   different in value?\n\n");

  return e1;
}


int main ()
{
  int a = 5;
  int b[] = {8, 14, -7, 128, 12};
  int c = 10;
  int d = 14;

  printf ("1) Locations...\n");
  printf ("   Use these locations to sketch the stack.\n");
  printf ("     &a: %x\n", &a);
  printf ("     &b: %x\n", &b);
  printf ("     &c: %x\n", &c);
  printf ("     &d: %x\n\n", &d);
  
  printf ("2a) Values:\n");
  printf ("   Why does b != 8?\n");
  printf ("     a: %x\n", a);
  printf ("     b: %x\n", b);
  printf ("     c: %x\n", c);
  printf ("     d: %x\n\n", d);

  printf ("2b) Values:\n");
  printf ("   What memory address is *b accessing?\n");
  printf ("     *b: %x\n\n", *b);
  
  printf ("3) Notice that the following increase by 4 each, why?\n");
  printf ("     &(b[0]): %x\n", &(b[0]));
  printf ("     &(b[1]): %x\n", &(b[1]));
  printf ("     &(b[2]): %x\n", &(b[2]));
  printf ("     &(b[3]): %x\n\n", &(b[3]));

  printf ("4) Pointers can be added, but the addition might have interesting results.\n");
  printf ("   Explain why b + 1 != b + 1 in the normal way of thinking about addition.\n");
  printf ("     b:   %x\n", b);
  printf ("     b+1: %x\n", b+1);
  printf ("     b+2: %x\n", b+2);
  printf ("     b+3: %x\n\n", b+3);
  
  foo (a, b, &c);
}

Upvotes: 0

Views: 202

Answers (1)

Bryan R
Bryan R

Reputation: 111

As @trentcl commented, apparently the parameters a1, b1, and c1 are being passed via registers and not on the stack. Editing foo to take an additional 8 dummy parameters forces a1, b1, and c1 to be passed as parameters on the stack with an address higher than the local parameters.

Upvotes: 1

Related Questions