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😀
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😀