• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question about standard c++ strings

Chu

Banned
Hello all. My first attempt at using the standard libraries, and, well, string seems the best place to start.

Here is a snippet of code:

char* fixme(const char* input_string){

string returnMe = input_string;

char* c_string = (char*)malloc((returnMe.size() + 1) * sizeof(char*));
//read comment below about the line that used to be here
strcpy(c_string, returnMe.c_str());
return c_string;
}

This program, as written, is both useless and innefficent 🙂 The thing is though, let's say for some reason I wanted to keep it like this instead of using c_str() (the original purpose of this was to test something about c_str(), but the fact it reuturns a const char* instead of a char* was annoying). The commented out line should get rid of the string, since I no longer need it. Problem is, delete *returnMe gives me the lovely error:

"regmask.cc", line 30: Error: The operation "* std::basic_string<char, std::char_traits<char>, std::allocator<char>>" is illegal.

and free doesn't work either. How exactly are you supposed to reclaim the memory from your strings, or is there something about memory allocation in the std libraries in general i'm overlooking?

-Charles😀
 
You didn't declare the string with the 'new' operator, so it's on the stack and will be destroyed when the function returns. You can only delete variables created with the 'new' operator and you can only free() memory allocated with mallo().
 
Nothinman pretty much answered all your questions, but I thought I'd add one thing.

Don't allocate memory on the heap inside a method and return a pointer to said memory to the caller. Relying on the caller to free/delete memory allocated by methods it calls is poor practice. Either take advantage of deterministic finalization with your class or require the caller to pass in its own buffer thereby making the need to free/delete explicit. If you're doing it the "C++ way" (i.e. not coding C++ just like C), do the former; else, do the latter.
 
Originally posted by: Nothinman
You didn't declare the string with the 'new' operator, so it's on the stack and will be destroyed when the function returns. You can only delete variables created with the 'new' operator and you can only free() memory allocated with mallo().

Ahhh, I thought there was an implicit malloc going on in there, since a char* isn't a string, and there is no formal constructor (i.e. string returnme = new string(input_string)). This is so much easier in java :\

-Chu
 
Ahhh, I thought there was an implicit malloc going on in there, since a char* isn't a string, and there is no formal constructor

Maybe not an implicit malloc persay, but there IS an implicit constructor, which probably calls malloc. And an implicit destructor which no doubt calls free() in it's implementation that gets called when the string goes out of scope.

It's basically the same as if there is a background pointer variable, call it p_returnMe, and this line: string returnMe = input_string; does the following behind the scenes:

p_returnMe = new string(input_string);
&returnMe = p_returnMe; //YOU can't assign a value to &returnMe but the compiler can

The reason you got the error was because returnMe is an object, not a pointer, so *returnMe makes no sense.
If you tried delete (string *)(&returnMe) it would likely have a runtime rather than compile time error. (note the cast to string* needed because &returnMe is actually a const string *)

When you exit the function and returnMe goes out of scope it's like delete &returnMe (or delete p_returnMe) is called behind the scenes.


BTW, the function you wrote already exists in a simpler form: strdup().

One more note: Yes, that error message looks like gibberish even to an experienced C++ programmer.
To interpret it what you need to know is that
std::basic_string<char, std::char_traits<char>, std::allocator<char>> is the same thing as std::string (or just string for short).

C++ strings are actually a template instantiation.
 
Back
Top