Parameters placement on stack in C

I am trying to illustrate the reverse order of parameters on the stack when passed to a function in C:

#include <stdio.h>

void  p(int p1, int p2, double p3)
{
        printf("params:\n"
                        "1) %p offset = %li\n"
                        "2) %p offset = %li\n"
                        "3) %p\n",
                        (void *)&p1,    (void *)&p1 - (void *)&p2,
                        (void *)&p2,    (void *)&p2 - (void *)&p3,
                        (void *)&p3);
}

int main(int argc, char *argv[])
{
  struct A a = {10,11,12, {1,2,3}};

        p(-1, 0, +1);
        return(0);
}

Result is:

params:
1) 0x7ffe2f20afac offset = 4
2) 0x7ffe2f20afa8 offset = 8
3) 0x7ffe2f20afa0

This is as expected on the 64 bit system (Ubuntu 19.04)

When I pass a structure as a parameter the stack looks puzzling to me:

#include <stdio.h>
struct A {
        int     n1;
        int     n2;
        int     n3;
        int     arr[3];
};

void    p(int p1, struct A p2, double p3)
{
        printf("params:\n"
                        "1) %p offset = %li\n"
                        "2) %p offset = %li\n"
                        "3) %p\n",
                        (void *)&p1,    (void *)&p1 - (void *)&p2,
                        (void *)&p2,    (void *)&p2 - (void *)&p3,
                        (void *)&p3);
}

int main(int argc, char *argv[])
{
  struct A a = {10,11,12, {1,2,3}};
        p(-1, a, +1);
        return(0);
}

Now result is:

params:
1) 0x7ffee5f7ddbc offset = -20
2) 0x7ffee5f7ddd0 offset = 32
3) 0x7ffee5f7ddb0
 

Parameter 3 is close to 1 and parameter 2 is not between 1 and 3? offsets look wrong to me.Please shed some light here, thanks in advance.
The compiler is gcc 7.4.0

The standards don't specify how parameters are placed on the stack when a function is invoked and any code that assumes that parameters are placed on the stack in the order given (or in the reverse of the order given) in the function declaration is highly likely to fail on some operating systems on some hardware.

When an operating system is ported to or designed from scratch, the compiler group, the link editor group, and the operating system group will study the hardware design documents and agree on a scheme that they believe will be fast and conserve space on that hardware for the operating system being designed. Note that the scheme may well have exceptions that apply alternative behaviors when a function is invoked that takes a variable number of parameters (e.g., printf() ). The only portable way to write functions like printf() is to use the macros defined in the Standard C header <stdarg.h> (i.e., va_start , va_arg , va_copy , and va_end ).

3 Likes

Thank you! That is so interesting!

Just for a comparison I run that code where I pass a structure as 2nd parameter on Solaris 11, here is the output, which is quite different

params:
1) feffed00 offset = -4
2) feffed04 offset = 28
3) feffece8

Was that Solaris 11 on Sparc or on Intel? The results might be different. :slight_smile:

Could it be that Ubuntu is arranging parameters on the stack based on their sizes? Weird indeed.

It was Solaris 11 on Intel

Probably, placement has to do with word boundaries. Which vary with different OS and hardware. As Don mentioned clearly. gcc has options for packing objects in memory. try gcc -Q -v inputfilename.c - assuming that is what you used. Be prepared for a lot of information on your screen.

1 Like