Please be
VERY CAREFUL with this code.
Consider
// THIS IS VERY VERY WRONG!!!!
char * getName(){
char buf[YOUR_ARRAY_SIZE];
sprintf(buf,name);
return &buf[0];
}
The code above is a runtime error, the problem is the you have a local variable (buf) and that goes out of scope at the end of the function. BUT you are passing the pointer to that memory, which you expect to use!!! It is possible/likely that sometime later the program will use that space for something else. Then you will have corrupted the string you are looking at, or even worst you might actually change it!!!!
The orginal is correct, if you want a copy (to manipulate) then make a local copy out of the methods. If you want to change a buffer in the class use a class method.
Sorry for the rant but this kind of error, is normally (a) Very difficult to debug, (b) dormant for a long while so it ends up in customers code.
Last edited by StuXYZ; Jan 30th, 2009 at 5:14 pm.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
Offline 659 posts
since Nov 2008