public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C calling conventions: how to pass a struct parameter?
@ 2004-04-22 17:01 Cezar Harabula
  2004-04-22 17:57 ` Eljay Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: Cezar Harabula @ 2004-04-22 17:01 UTC (permalink / raw)
  To: gcc-help, gcc-help-info

I simply cannot find this information! I have a procedure that must
receive a struct. How to pass a structure parameter from a caller to my
procedure, if the processor is an x86?
In C++: how to pass an object?

Thank you.
Cezar

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C calling conventions: how to pass a struct parameter?
  2004-04-22 17:01 C calling conventions: how to pass a struct parameter? Cezar Harabula
@ 2004-04-22 17:57 ` Eljay Love-Jensen
  2004-04-23  7:38   ` Cezar Harabula
  0 siblings, 1 reply; 4+ messages in thread
From: Eljay Love-Jensen @ 2004-04-22 17:57 UTC (permalink / raw)
  To: Cezar Harabula, gcc-help, gcc-help-info

Hi Cezar,

(Hey, "Cezar", cool name.)

This should work:

struct Foo { int a,b,c; };

void PrintFoo(struct Foo foo)
{
   printf("Foo:%d,%d,%d\n", foo.a, foo.b, foo.c);
}

That's a pass-by-value.

Here's pass-by-pointer:

void PrintFoo(struct Foo* foo)
{
   printf("Foo:%d,%d,%d\n", foo->a, foo->b, foo->c);
}

Here's pass-by-reference (C++ only):

void PrintFoo(Foo const& foo)
{
   printf("Foo:%d,%d,%d\n", foo.a, foo.b, foo.c);
}

Or am I misunderstanding your question?  Such as "what is the memory layout 
on the stack of the structure?", which depends on the aforementioned 
pass-by-???? method employed.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C calling conventions: how to pass a struct parameter?
  2004-04-22 17:57 ` Eljay Love-Jensen
@ 2004-04-23  7:38   ` Cezar Harabula
  2004-04-24  2:20     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Cezar Harabula @ 2004-04-23  7:38 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help, gcc-help-info

Thank you for your answers. Unfortunately, I was not clear enough. I 
want to know what happens in the registers and in the stack when a 
procedure like

void PrintFoo(struct Foo foo)

is called. There is no universal C calling convention set for x86,  so I 
am interested in how GCC usually does.

What about a struct return value:
struct bar Function() ?

Thanks.
Cezar


Eljay Love-Jensen wrote:

> Hi Cezar,
>
> (Hey, "Cezar", cool name.)
>
> This should work:
>
> struct Foo { int a,b,c; };
>
> void PrintFoo(struct Foo foo)
> {
>   printf("Foo:%d,%d,%d\n", foo.a, foo.b, foo.c);
> }
>
> That's a pass-by-value.
>
> Here's pass-by-pointer:
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C calling conventions: how to pass a struct parameter?
  2004-04-23  7:38   ` Cezar Harabula
@ 2004-04-24  2:20     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2004-04-24  2:20 UTC (permalink / raw)
  To: Cezar Harabula; +Cc: gcc-help

Cezar Harabula <cezar.harabula@open-plug.com> writes:

> Thank you for your answers. Unfortunately, I was not clear enough. I
> want to know what happens in the registers and in the stack when a
> procedure like
> 
> void PrintFoo(struct Foo foo)
> 
> is called. There is no universal C calling convention set for x86,  so
> I am interested in how GCC usually does.
> 
> What about a struct return value:
> struct bar Function() ?

The easy way to find out is to write some code and look at the result
when it is compiled using -S.  The results will change if you use the
-mregparm option.

Structures are normally passed by pushing the value of the structure
on the stack.

Structures are normally returned by arranging for the caller to pass
the address of a memory area as a hidden first argument.  The function
then stores the return value into that memory area.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-04-24  2:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-22 17:01 C calling conventions: how to pass a struct parameter? Cezar Harabula
2004-04-22 17:57 ` Eljay Love-Jensen
2004-04-23  7:38   ` Cezar Harabula
2004-04-24  2:20     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).