How does C++ return structures?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

How does C++ return structures?

Post 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
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: How does C++ return structures?

Post 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]
Last edited by eddyb on Mon Dec 08, 2008 1:23 pm, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How does C++ return structures?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: How does C++ return structures?

Post 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).
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How does C++ return structures?

Post by JamesM »

You're both right. CodeCat in the x86 case and Combuster for his observation about the cross-architecture case.
LoseThos
Member
Member
Posts: 112
Joined: Tue Oct 30, 2007 6:41 pm
Location: Las Vegas, NV USA
Contact:

Re: How does C++ return structures?

Post 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.
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: How does C++ return structures?

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: How does C++ return structures?

Post 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
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: How does C++ return structures?

Post 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
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: How does C++ return structures?

Post 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
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: How does C++ return structures?

Post 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.
Post Reply