Skip to main content
GameDev.net gamedev.net
🔒 Locked

returning a string (why doesnt this work)

Started by wannabean333 Aug 7, 2004 at 4:20 PM 4 replies 700+ views
Original Post
wannabean333
wannabean333
hey i wrote this function for use in my game and the program runs but it crashes before anything shows up ill just give it all to you example of usage:

strcpy(buffer,FileName(MAP_PREFIX,"itsamap",MAP_SUFFIX));
CORE_Load_Map(buffer);
definition:

char *FileName(char *prefix, char *filename, char *suffix)
{
	char return_name[96];
	strcpy(return_name, prefix);
	strcat(return_name, filename);
	strcat(return_name, suffix);
	return return_name;
}
defines:

// map
#define MAP_PREFIX "maps\\"
#define MAP_SUFFIX ".map"
Fruny
Fruny
You return a pointer to a local variable. The problem is that when your function returns, its stack frame is popped off and the local variable is discarded. Your pointer is then left pointing to an invalid memory location (if you are lucky) or into another function's stack frame (if you are unlucky).
"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
Ready4Dis
Ready4Dis
Becuase it's returning the address to the string. The problem is, the string only exists within the function itself, once it exits, the memory is de-allocated, so you cannot reference it anymore. There are 2 easy solutions:

Pass the pointer to a string into the function instead of returning one:

void FileName(char *Dest, char *pre, char *fname, char *suffix){}char Test[96];FileName(Test,MAP_PREFIX,"itsamap",MAP_SUFFIX);


Then you can use Test :). Another method would be to dynamically allocate the memory within the function, then delete it when it's no longer needed.

char *FileName(char *pre, char *fname, char *suffix){ char *Test = new char[96] return Test;}char *Buff = FileName(MAP_PREFIX,"itsamap",MAP_SUFFIX);delete[] Buff; //Delete it now!
Alpha_ProgDes
Alpha_ProgDes
i would make return_name a static variable.
if you leave the way it is, the array loses scope as soon as you exit the function. and that might lead to problems.

also i think backslashes have to made a certain when putting it into a string. ie. you just can't have ".map\\" i think it's ".map\\\\", but i could be wrong.

and are you sure that you're adding the correct strings as in not doing this:

word1 = "bob\0"
word2 = "went\0"
word3 = "home\0"

array = "bob\0"
strcat + word2 = "bob\0went\0"

what i'm trying to say is: are the correct characters going into the array and being terminated correctly.
and is the array checked for an overflow?
Beginner in Game Development?  Read here. And read here.  
Fruny
Fruny
Actually, the best solution would be to use a real C++ string.

#include <string>using namespace std;string FileName(const string& prefix,                const string& filename,                const string& suffix){   string tmp = prefix;   tmp += filename;   tmp += suffix;   return tmp;}


BTW, wannabean333, your code is highly unsafe, if the total length is over 95 characters, you've got an array overrun.
"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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.