Closed GoogleCodeExporter closed 9 years ago
It doesn't return a pointer, instead, it returns a struct. This results in a
bit of extra copying, but 12 bytes is not that big a struct.
However, it could be better to change it anyway. Some compilers (e.g. sdcc)
don't support returning structs.
Original comment by Petteri.Aimonen
on 1 Aug 2012 at 7:38
right, the headline is wrong, but my bitching aint ;)
the issue is: the returned struct is allocated on the stack, and by the time
it's assigned there's no guarantee the stack is still intact:
see second answer in:
http://stackoverflow.com/questions/4911288/allocate-struct-from-function-in-c
so the solution is to allocate the memory for the struct in the caller, and
pass a pointer/reference to the struct so pb_istream_from_buffer can fill in
the details
Original comment by haberl...@gmail.com
on 1 Aug 2012 at 8:10
But in turn, see the answers in:
http://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c
(Or this answer in your link: "Return a copy of the struct (this is OK for
small structs)")
It's perfectly valid C to return a struct by-value. The caller makes a copy of
it on the stack, just like if you returned an integer.
I agree that there would be a problem with "return &stream;". However, "return
stream;" is ok.
Original comment by Petteri.Aimonen
on 1 Aug 2012 at 8:32
you win ;)
issue closed
Original comment by haberl...@gmail.com
on 1 Aug 2012 at 8:35
:)
Original comment by Petteri.Aimonen
on 1 Aug 2012 at 8:39
Issue 95 has been merged into this issue.
Original comment by Petteri.Aimonen
on 12 Dec 2013 at 8:31
Original issue reported on code.google.com by
haberl...@gmail.com
on 1 Aug 2012 at 7:10