Page 1 of 1

How does C++ return structures?

Posted: Mon Dec 08, 2008 11:23 am
by sebihepp
Hello,

normally the value is returned in eax. But what's up, if a structure is bigger than let's say 40 Bytes?
The only way I think is with a pointer.

Greetings
Sebihepp

Re: How does C++ return structures?

Posted: Mon Dec 08, 2008 11:32 am
by eddyb
sebihepp wrote:Hello,

normally the value is returned in eax. But what's up, if a structure is bigger than let's say 40 Bytes?
The only way I think is with a pointer.

Greetings
Sebihepp
[stupid]
i know the stack is used for operands and return.
so, if it is some_struct blah(blah1, blah2), some_struct should be in stack. but, if it is some_struct * blah(blah1, blah2), a pointer to some_struct should be in the stack.
[stupid]

Re: How does C++ return structures?

Posted: Mon Dec 08, 2008 1:21 pm
by Combuster
Not true, especially not in the average case
On x86/C calling convention, the return value is in eax/ax/al (for int/short/char and pointers) and arguments are on the stack; not sure about floats and structs.
On amd64/Cdecl arguments use register conventions for the first few variables and the stack for the remainder. Return is via registers.
On several other risc machines there are enough argument passing registers that the stack is practically only used for the return address.

Re: How does C++ return structures?

Posted: Mon Dec 08, 2008 2:18 pm
by CodeCat
No, he is right. Some compilers do in fact pass a hidden pointer to the struct as a parameter to be filled by the function. See here for more information on C++ call conventions:

http://www.angelcode.com/dev/callconv/callconv.html

As you can see, C++ calling conventions are not entirely standardised, and different compilers do things in different ways. Win32 versions of GCC have been modified to fit the VC++ call convention so that compatibility remains across DLL calls. But I believe the cross compiler that most people here use uses the 'g++ / Linux' way, even on Windows (which is good because it's somewhat simpler).

Re: How does C++ return structures?

Posted: Mon Dec 08, 2008 7:29 pm
by JamesM
You're both right. CodeCat in the x86 case and Combuster for his observation about the cross-architecture case.

Re: How does C++ return structures?

Posted: Mon Dec 08, 2008 9:47 pm
by LoseThos
I don't know the answer, but would guess it's on the stack. The interesting thing is it must be above the return address in the region with parameters. It cannot be below the return address or it would be clobbered if an interrupt occurred after returning.

Re: How does C++ return structures?

Posted: Mon Dec 08, 2008 11:34 pm
by TyrelHaveman
LoseThos wrote:I don't know the answer, but would guess it's on the stack. The interesting thing is it must be above the return address in the region with parameters. It cannot be below the return address or it would be clobbered if an interrupt occurred after returning.
I read something VERY recently (was it the ABI or something? I cannot recall) that said that structures to return (or other things that don't fit in EAX) are in fact passed as a hidden parameter on the stack in C (and I presume in C++ as well). For example, if you have:

Code: Select all

struct coord { int x; int y };
struct coord GetLocation(int something) {  .. .. do stuff, return a struct coord ... }
The caller will do something like

Code: Select all

sub esp, 8   ; room for the struct to return
push var     ; the parameter 'something'
call _GetLocation
add esp, 4   ; remove the parameter 'something' off the stack
; now use the 8 bytes at [esp] as the return value...
I wish I could remember where I read this (it was within the past two weeks) so I could cite my source... but I cannot recall. If I do, I'll post an update.

Re: How does C++ return structures?

Posted: Tue Dec 09, 2008 3:28 am
by AJ
Hi,

My understanding is that structs and classes returned by value are returned on the stack of the calling function - a quick disassembly shows this to be the case.

If you think about it, this makes sense. On the x86, local variables are stored on the stack. When you return a struct or class by value, it becomes a local variable of the calling function, so the sensible place to store it is on the stack of the caller.

Cheers,
Adam

Re: How does C++ return structures?

Posted: Tue Dec 09, 2008 8:37 am
by DeletedAccount
Hi,
Think this is implementation specific , for example in Turbo C , the values of the structure seems to be stored in registers if the stucture is quite small or the stack otherwise
Regards
Shrek

Re: How does C++ return structures?

Posted: Tue Dec 09, 2008 9:43 am
by Craze Frog
Hello,

normally the value is returned in eax. But what's up, if a structure is bigger than let's say 40 Bytes?
The only way I think is with a pointer.

Greetings
Sebihepp
It is implementation specific. If the structure is only 40 bytes it can theoretically be returned in registers. The function may even be inlined if it's deemed safe by the compiler. In that case there won't be any true return at all.
Combuster wrote:Not true, especially not in the average case
On x86/C calling convention, the return value is in eax/ax/al (for int/short/char and pointers) and arguments are on the stack; not sure about floats and structs.
On amd64/Cdecl arguments use register conventions for the first few variables and the stack for the remainder. Return is via registers.
On several other risc machines there are enough argument passing registers that the stack is practically only used for the return address.
Cdecl is not used for 64-bit architecture. The only possible calling convention is fastcall.
AJ wrote:Hi,

My understanding is that structs and classes returned by value are returned on the stack of the calling function - a quick disassembly shows this to be the case.
Don't just rely on experiments, read the actual rules for the calling convention used. Try to disassemble this and see if the struct is really returned on the stack - here it isn't. (It's returned in eax:edx.)

Code: Select all

struct s64bit {
    int first;
    int second;
};

struct s64bit return64bit(int x) {
    struct s64bit ret;
    ret.first = x;
    ret.second = x >> 16;
    return ret;
}

int main (int argc, char *argv[]) {
    struct s64bit x;
    asm ("___tssst:");
    x = return64bit(argc);
    return x.first;
}

It seems to me like everyone in this thread is just guessing. Read this instead: www.agner.org/optimize/calling_conventions.pdf

Re: How does C++ return structures?

Posted: Tue Dec 09, 2008 6:40 pm
by tantrikwizard
Combuster wrote:..not sure about floats and structs...
It depends on compiler and calling convention. Some compilers return precision numbers on the FPU. Some calling conventions use a series of registers instead of pushing everything on the stack.