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

Started by
5 comments, last by Anachronism 21 years, 3 months ago
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
Advertisement
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...)


[*]<br> <br><br>–<br>Simon O''Connor<br>Creative Asylum Ltd<br><A HREF="http://www.creative-asylum.com">www.creative-asylum.com</A>

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

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); } 
"after many years of singularity, i'm still searching on the event horizon"
quote:Original post by S1CA
[*]<br> <hr height=1 noshade></SPAN></BLOCKQUOTE> <br><br>Thread-safety ? <img src="smile.gif" width=15 height=15 align=middle><br> <br><br><hr><tt><small>[ <a href="http://www.gamedev.net/reference/start_here/">Start Here !</a> | <a href="http://www.tuxedo.org/%7Eesr/faqs/smart-questions.html">How To Ask Smart Questions</a> | <a href="http://www.accu.org/bookreviews/public/reviews/0hr/">Recommended C++ Books</a> | <a href="http://www.parashift.com/c++-faq-lite/">C++ FAQ Lite</a> | <a href="http://www.function-pointer.org/">Function Ptrs</a> | <a href="http://search.freefind.com/find.html?id=8028742&m=0&p=0">CppTips Archive</a> ]<br>[ <a href="http://www.gamedev.net/reference/programming/features/orgfiles/">Header Files</a> | <a href="http://www.wotsit.org">File Format Docs</a> | <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/LNK2001.asp">LNK2001</a> | <a href="http://www.sgi.com/tech/stl/">C++ STL Doc</a> | <a href="http://www.stlport.org">STLPort</a> | <a href="http://www.bloodshed.net/devcpp.html">Free C++ IDE</a> | <a href="http://www.boost.org">Boost C++ Lib</a> | <a href="http://www.dinkumware.com/vc_fixes.html">MSVC6 Lib Fixes</a> ]</small></tt>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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];
Thanks for fixing my problem, and teaching me new things...

-Dennis
Heh, someone shoudl tell my C Programming Lecturer that, i told him and his resonse was "Well, its worked for me so far, so i see no reason to change it". Madness...

Member of the Unban Mindwipe Society (UMWS)

This topic is closed to new replies.

Advertisement