Re: Question about function call stack

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



With C, you can not really assume anything.

If you are trying to do this for one particular platform, then you can look at the code generated and eventually figure out how to do what you want to do. If you are doing 32 bit and 64 bit on one platform, then you could #ifdef the two cases.

But if you truly want portable code, then you need to either use varargs or stdargs. stdargs is part of the C standard I believe. It is pretty easy to use.

I get the idea that stdard is preferred but I see varag used more often in code.

Google "stdarg" will get you to a man page. Your system probably has a man page for stdarg on it.

HTH
pedz


On Jan 17, 2006, at 9:32 AM, Emmanuel Pacaud wrote:

Hi,

In a project I'm working on, I've to fix an issue which is dependant on the target platform.

You'll find below a code snippet that shows the issue. When compiled for a 32 bit platform, with gcc 3.2.2, I get the expected behaviour, which is the following output:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

But, if compiled for a 64 bit platform, with gcc 3.4.3, output is:

1 548682058152 4194884 0 -72340172838076673 0 0 1 2 3
0 1 2 3 4 5 6 7 8 9

Here's my question:

Is the assumption made in code snippet that pushing an ulong array on the stack, then retrieving values via function arguments correct ?

Regards,

      Emmanuel.

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

#define SIZE 10

void
f (ulong v1, ulong v2, ulong v3, ulong v4, ulong v5, ulong v6, ulong v7, ulong v8, ulong v9, ulong v10)
{
	printf ("%li %li %li %li %li %li %li %li %li %li \n",
		v1, v2, v3, v4, v5, v6, v7, v8, v9, v10);
}

typedef struct {
	ulong array[SIZE];
} Stack;

void
g (Stack stack)
{
	int i;

	for (i = 0; i < SIZE; i++)
		printf ("%li ", stack.array[i]);
	printf ("\n");
}

int
main (int argc, char *argv[])
{
	void (*function_ptr) ();
	Stack stack;
	int i;

	for (i = 0; i < SIZE; i++)
		stack.array[i] = i;
	
	function_ptr = f;
	(*function_ptr) (stack);
	function_ptr = g;
	(*function_ptr) (stack);

  	exit(0);
}


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux