Archived

This topic is now archived and is closed to further replies.

Anachronism

Receiving junk (??) when returning a string from a function ...

Recommended Posts

Hello... The score for my game is an integer, and I have a function that prints text as strings... I''m trying to convert the score to a string, and return it from a member function of my class.... For some reason the only thing that ever shows up is a "g" and not the score integer... Any ideas why? Here''s the code that returns the score as a string: char* getScoreAsString(void) { char buff[11]; return itoa(score, buff, 10); } This is how I am passing it into my print text function: printString(listbase, gf->getScoreAsString()); I know the printString function works because if i substitute what I have there with "2003" it works wonderfully. Problem must be in how I''m returning the string. Help is appreciated! Thanks... -Dennis

Share this post


Link to post
Share on other sites
char buff[11];

is a LOCAL variable, it will only be valid for as long as the program is inside getScoreAsString() - as soon as you exit that function, anything can happen to that memory - it might remain ok if you''re lucky; it might get totally trashed; it might get partially trashed. The reason it''ll be trashed is local variables used in any function or created AFTER getScoreAsString() has returned will use the SAME memory.


As a simple solution make "char buff[11];" global - or better, rethink your design so that you aren''t returning locally allocated memory from a function.
Declaring it as a static would make the code look a bit nicer than a global (same thing* as a global after compilation though...)


[* within context of reply to post - before anyone pulls me up on scoping, mangling, PE BSS storage vs stack storage etc]


--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Functions returning pointers are dangerous... especially in your context. Don''t treat char* as string, because it isn''t. Integer works because it isn''t a pointer.

Give std::string a try.

If you still wanna try on char*, I suggest this approach:

void getScoreAsString(char *outbuf, int score)
{
itoa(score, buff, 10);
}

Share this post


Link to post
Share on other sites
quote:
Original post by S1CA
[* within context of reply to post - before anyone pulls me up on scoping, mangling, PE BSS storage vs stack storage etc]



Thread-safety ?



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
The other posters are correct (I especially encourage the use of std::string), but for a straight answer to your question: char buff[11]; allocates the array statically, which means that the memory is deallocated at function exit. If you wish to return a valid pointer, you will have to allocate the memory dynamically: char* buff = new char[11];

Share this post


Link to post
Share on other sites