Re: [cc65] returning structs

From: Groepaz <groepaz1gmx.net>
Date: 2009-08-10 11:40:29
On Montag 10 August 2009, Oliver Schmidt wrote:
> Hi Groepaz,
>
> >> I know that you cant return a struct by value.  Is there any way around
> >> this?  If I return a pointer to the struct, the values are lost upon
> >> leaving the function.  Can anyone provide an example workaround?
> >
> > make it return a pointer, and then copy it to a local struct:
> >
> > memcpy(&localstruct,functhatreturnsstruct(),sizeof(localstruct));
> >
> > (thats pretty much almost exactly what a compiler would do too)
>
> Maybe it's woth mentioning that your proposal presumes that memcpy()
> a) is inlined / is a specially designed intrinsic / <you name it>
> b) doesn't use the stack itself
> because otherwise calling it could overwrite the data you want to
> copy. From that perspective I'd rather classify this approach as hack
> than as workaround.

hu? how would calling memcpy destroy the data that was passed to it? if it 
does that its just broken. what you say might be a problem if 
thefuncthatreturnsstruct() actually DOES return a struct by value - i wouldnt 
know since i never use this particular c feature. but even then i would 
expect memcpy not to destroy any data that is beeing passed to it :)

-- 

http://www.hitmen-console.org    http://magicdisk.untergrund.net
http://www.pokefinder.org        http://ftp.pokefinder.org

Wenn man ein zweite Chance im Leben bekommt, sollte man immer daran denken das 
man seine erste bereits versaut hat.

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Mon Aug 10 11:40:10 2009

This archive was generated by hypermail 2.1.8 : 2009-08-10 11:40:12 CEST