Original Post
Okay guys, I've racked my brains, looked in my c++ books, and searched google, but I couldn't find a solution to my problem.
I want to return a character array without having to delete the return value.
Here is a simplified version of my function that doesn't work. The buffer array is on the stack and goes out of scope when my function returns so all I get is a bunch of garbage. Yes, len is defined and has a value, but it's not important.
//Doesn't work:
char* GetText(){
char buffer[len+1];
return buffer;
}
//But I don't want to do this:
char* GetText(){
char* buffer;
buffer = new char[len+1];
return buffer;
}
//because I would have to do this everytime I use said function:
char* ret = GetText();
// use return value
delete ret;
If I don't delete the pointer I get a memory leak. Any ideas? I don't want to use std::string, because I feel as though I don't need to use if for something so simple.
Thanks a bunch guys!
CD
Jesus is Lord!!
[edited by - CD579 on December 30, 2003 7:00:27 PM]