How does C++ return structures?
-
- Member
- Posts: 195
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
How does C++ return structures?
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
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?
[stupid]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
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.
- Combuster
- 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?
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.
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?
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).
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?
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?
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.
- TyrelHaveman
- Member
- Posts: 40
- Joined: Thu Sep 20, 2007 11:20 pm
- Location: Bellingham, WA
- Contact:
Re: How does C++ return structures?
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: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.
Code: Select all
struct coord { int x; int y };
struct coord GetLocation(int something) { .. .. do stuff, return a struct coord ... }
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...
Re: How does C++ return structures?
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
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
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: How does C++ return structures?
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
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
-
- Member
- Posts: 368
- Joined: Sun Sep 23, 2007 4:52 am
Re: How does C++ return structures?
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.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
Cdecl is not used for 64-bit architecture. The only possible calling convention is fastcall.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.
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.)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.
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
-
- Member
- Posts: 153
- Joined: Sun Jan 07, 2007 9:40 am
- Contact:
Re: How does C++ return structures?
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.Combuster wrote:..not sure about floats and structs...