I hope this question is not too subjective.. all I want is good design that would prevent memory leaks happening later in my code. I coulnd't find any question on SO. I always found questions about what to do after allocating data in function which is not my case.
Also for some reason I am not using newer C++ standards like C++11 (shared pointers).
Let me demonstrate by example:
I have logic of data buffering which are later being sent. The buffering is done in one class and sending in another class.
In one point of code I am taking some data from buffer, process it (check the type of data etc) and then send it with function send:
bool send_data(char *data, size_t data_length) {
The data are consumed and are no longer needed. Shall I free them in the send_data or shell I leave that to the caller?
Free it inside:
bool send_data(char *data, size_t data_length) { //... process data ... send(data, data_length, ...); delete[] data; }
Leave it and let the caller free it:
send_data(data,data_length); delete[] data;
Or is there a design flaw and I should do something totally different?
The reason for not using C++11 is that the code is big & old - should I rewrite it completely? But I am considering to rewrite just some parts of the code because something is better then nothing.
And also the usage of some pointers spans lots of places of code I would have to change them all. Sometimes its not so easy to find them all because the usage may be hidden by casting and the buffering etc.
The data buffering is important. I have lot of data in buffers not just one char array. I am not sure if the data can be made static as some of you have in answers.. I will think about it.