Since I am a newbie, I just want to ask more experienced programmers in C++ is this a good C++ practice. I have a function which returns *char
. This function is in dll.
I have defined global *char
variable, and that is what is returned. I am using libcurl for POST request method. Since every response is in different length I am using new
. When you are using new, it also necessary to use delete
, and I want to check am I using delete and pointer correctly?
char *response
struct MemoryStruct {
char *memory;
size_t size;
};
char *function() {
//...
// libcurl code
//...
// is this part of a code a good practice?
if (response == nulptr) {
response = new char[cunk.size];
memcpy(response, chunk.memory, chunk.size);
} else {
delete[] response;
response = new char[chunk.size];
memcpy(response, chunk.memory, chunk.size);
}
//...
// libcurl cleanup
//...
return response;
}
Is this OK or is there another (better) way to do this?
Thank you for any help.
EDIT: I forgot to mention that result is not returned to C++, it is returned to Clarion. So I cannot use smart pointers or string.