help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] gst_string_to_oop with local string


From: Roland Plüss
Subject: Re: [Help-smalltalk] gst_string_to_oop with local string
Date: Wed, 11 Nov 2009 20:13:15 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20091031)


Paolo Bonzini wrote:
> On 11/11/2009 12:58 AM, Roland Plüss wrote:
>> char buffer[ 50 ];
>> sprintf( ( char* )&buffer, "(%f,%f,%f,%f)", csobject.color.r,
>> csobject.color.g, csobject.color.b, csobject.color.a );
>> return gst_string_to_oop( buffer );
>>
>> Returns garbage in smalltalk ( function returns OOP by the way and is
>> mapped with #smalltalk as return value ). As I get from the
>> documentation this function only "maps" the string and does not make a
>> copy.
>
> No, that's not true.  I think you are overrunning the buffer (besides,
> (char*)&buffer is simply "buffer").  Try asprintf, like
Nope. The signature of sprintf is "int sprintf(char *str, const char
*format, ...)". buffer thought is of type "char (*)[50]". C++ is not
going to compile ( error: cannot convert 'char (*)[50]' to 'char*' for
argument '1' to 'int sprintf(char*, const char*, ...)' ). Hence the type
cast is required to get a proper and correct code which is accepted by a
strict compiler. Another solution would be "sprintf( &buffer[0], ... )"
which is again valid since &buffer[0] is of type "char*". I prefer
though myself the cast solution.

Otherwise I got it working now. Didn't pay attention that smalltalk
works with double instead of float. So a prior cCall involving a float
messed things up as smalltalk wrote a double over a float somewhere.
Changing that fixed it so no overrun in that one. I'm though most
probably going to change the code anyways to use a static allocated
buffer somewhere for formating. Prevents wasted allocations ( asprintf
not an optimal solution ).

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://epsylon.rptd.ch/ ,
http://www.moddb.com/games/4057/epsylon )
- Game Engine: Drag(en)gine ( http://dragengine.rptd.ch ,
http://www.moddb.com/engines/9/dragengine )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]