This is a long one. I have a function that loads some image data from disk. I’ve tried this with three different methods, one of which doesn’t work, and I’m wondering what the smartest method is.
Method 1:
I used to have it set up so that the loaded data was an argument to the function, which seemed to require allocating the space for it outside the function then passing a pointer to the allocated space. This requires knowing the size of the image ahead of time, so a helper function had to be made to get the image width and height first. The following is an example.
Pros: More explicit in the calling code that memory is being allocated and therefore should be deleted.
Cons: Need to know image size ahead of time.
// Data allocated outside the image, allocated space passed to function. This works.
// Notice that width & height are passed to the function.
size=(*width)*(*height);
image = new unsigned char[size];
void read_pgm(unsigned char *image, char *file_name, int width, int height){
// Code to read sizeof(char)*width*height bytes of data from the file into image
}
Method 2:
I thought it would be nice to have the function allocate its own data so I don’t need to pass it a size. If I tried to allocate the space for it in the function, it seemed to be lost after the function ended. In the following case, the function read_pgm runs fine, but if I then try to write that data to another file, my code crashes.
Pros: Don’t need to know image size ahead of time, no need to allocate data in calling code.
Cons: Doesn’t work. Also, if it did, would calling it repeatedly in a loop result in a memory leak if I wasn’t clearing image outside the function?
// Data allocated inside the image for a pointer passed to the function. This doesn't work.
void read_pgm(unsigned char *image, char *file_name, int *width, int *height){
size=(*width)*(*height);
image = new unsigned char[size];
// Code to read the data from the file into image
}
Method 3:
Here, the data is again allocated in the function but is handed back as a returned item. This works, i.e. I can then write the data out to another image fine. I don’t understand why this works and Method 2 does not.
Pros: Same as Method 2.
Cons: Same as Method 2 except this currently works.
// Data allocated in the function, and returned. This works.
unsigned char* read_pgm(char *file_name, int *width, int *height){
// Allocate data for the image
size=(*width)*(*height);
image = new unsigned char[size];
// Code to read the data from the file into image
return image; // Return pointer to the data
}
So, my questions are:
-
In a case like this, is it smarter to set the function up to allocate space itself so the calling code doesn’t need to supply the image size? Or is it smarter to allocate outside the function as a reminder that delete needs to be called on image at some point. Or am I wrong in thinking that this needs to be done? It seems like calling Method 2 or Method 3 in a loop would make a memory leak.
-
Why doesn’t Method 2 work?
Thanks.
Answer to question 2
In order to make it work you must pass a reference to the pointer
Otherwise you allocate memory and the COPY of the passed pointer points to it. The original pointer object doesn’t change.
Answer to question 1
Ever heard of smart pointers?
Smart pointers can be used to manipulate memory all by themselves. If you don’t want to use smart pointers for some inexplicable reason, such as pseudooptimization, then I think you described the pros and cons of all methods yourself – there’s always tradeoff. Up to you to decide