Hi
A function returning a struct is given a pointer where it copies the return value. Now, is there a language construct for returning a struct filled with zeroes?
And if a function's formal parameter is a struct, is it given by value or by address?
Cheers
c: functions returning a struct
Re:c: functions returning a struct
That wouldn't work, as you cannot return a struct by value that you received by pointer. You could perhaps return a copy of that struct, but that struct and the one pointed to by the pointer wouldn't be the same object.Adek336 wrote:A function returning a struct is given a pointer where it copies the return value.
You could make the function return the pointer to the struct...
AFAIK an automatically created struct is zero-initialized by default, but I'm not sure.Now, is there a language construct for returning a struct filled with zeroes?
Every good solution is obvious once you've found it.
Re:c: functions returning a struct
Thanks for you reply Solar
I think I was somewhat unclear.
it looked like main allocated v on stack and then passed a pointer to it using %eax, while myFun allocated u on stack, manipulate it and copy the contents of u to v.
I'm not sure, though. Well anyways I found a neat way for returning an empty struct:
In the mean-time, I decided my function would return a pointer as I do not need a copy of the bios32 directory service header.
Cheers
I think I was somewhat unclear.
Well, I mean, in the disassembly of something like the followingA function returning a struct is given a pointer where it copies the return value.
Code: Select all
struct x { int a; .. };
struct x myFun ()
{
struct x u;
u.a=1;
return u;
}
int main()
{
struct x v= myFun();
}
I'm not sure, though. Well anyways I found a neat way for returning an empty struct:
Code: Select all
struct x
{
int a;
a (int val) { memset(this, val, sizeof(x)); }
}
struct x NullX() {
return (struct x) 0;
}
Cheers
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:c: functions returning a struct
I don't have a source handy, but my gut says no.Solar wrote:AFAIK an automatically created struct is zero-initialized by default, but I'm not sure.Now, is there a language construct for returning a struct filled with zeroes?
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager