I see a lot of RAII example classes wrapping around file handles.
I have tried to adapt these examples without luck to a character pointer.
A library that I am using has functions that take the address of a character pointer (declared like get_me_a_string(char **x)). These functions allocate memory for that character pointer and leave it up to the end user of the library to clean it up in their own code.
So, I have code that looks like this...
char* a = NULL;
char* b = NULL;
char* c = NULL;
get_me_a_string(&a);
if(a == NULL){
return;
}
get_me_a_beer(&b);
if(b == NULL){
if(a != NULL){
free(a);
}
return;
}
get_me_something(&c);
if(c == NULL){
if(a != NULL){
free(a);
}
if(b != NULL){
free(b);
}
return;
}
if(a != NULL){
free(a);
}
if(b != NULL){
free(b);
}
if(a != NULL){
free(b);
}
It sounds like RAII is the answer for this mess that I have above. Could someone provide a simple C++ class that wraps a char* rather than a FILE*?
Thanks