deleting char**

Started by
6 comments, last by jarod83 19 years, 4 months ago
if i have following:

char** imgPaths;
imgPaths = new char*[FACE_COUNT];

how do i free the memory after use ? I have tried different ways, but it is not working...
Advertisement
Hello,

I assume you did this to allocate the memory:
char** imgPaths;imgPaths = new char*[FACE_COUNT];for in( i=0; i<FACE_COUNT; i++) {  imgPath = new char[some_size];}


Then you have to do:
for (int i=0; i<FACE_COUNT; i++) {  delete [] imgPaths;}delete [] imgPaths;


HTH,
Quote:Original post by Emmanuel Deloget
Hello,

I assume you did this to allocate the memory:
char** imgPaths;imgPaths = new char*[FACE_COUNT];for in( i=0; i<FACE_COUNT; i++) {  imgPath = new char[some_size];}


Then you have to do:
for (int i=0; i<FACE_COUNT; i++) {  delete [] imgPaths;}delete [] imgPaths;


HTH,


It does not help, I get runtime error saying "Debug Assertion Failed..."
Actualy I have the following:
Box::Box(..., char *img[FACE_COUNT]) {imgPaths = new char*[FACE_COUNT];imgPaths = img; // Array copy


So the question is how to delete imgPaths ?!?
if that is meant to represent a dynamic array of strings in c++ then do it in c++, use the standard library:

#include <string>#include <vector>#include <iostream>typedef std::vector<std::string> vec_of_strings;inline void foo(const vec_of_strings& vs) {   std::cout << vs[0] << " ... " << vs[1] << std::endl;}int main() {   vec_of_strings vs;   vs.push_back("hello");   vs.push_back("goodbye");   foo(vs);}


find out more here
imgPaths = img; // *NOT* an Array copy, this is a *POINTER* copy.

After this line, imgPaths (which is a pointer), points to the same object as img (which is also a pointer). Therefore,

1- The memory you allocated using new is lost (you don't have its address stored anywhere) and will eventually clog your memory.

2- The memory you try to delete is actually the same bit of memory that was passed to your function - which might or might not be available for deletion, especially if it was allocated on the stack.

To perform array copy using the = operator, use an stl deque or vector (in your case, a vector, since you might need contiguous memory allocation).
To perform a C-style array copy, use memcpy, although I do not recommend it.
Although snk_kid has pointed out the correct way of doing things, I thought I'd try to help you understand why your current version is not working (assuming you're code is as in your other thread, not this one). I assume you have something like:
char* img[n] = {"a string", "another string", ...};

or
char** img = new char*[n];image[0] = "a string";image[1] = "another string";...

In either case, when you call imgPaths = img you are copying a char* that was not dynamically allocated with a call to new[]. If it wasn't allocated by new[] it shouldn't be deleted with delete[]. In this case the strings are stored in the executable image. You do not need to manage them at all, they will be automatically unloaded with the rest of the executable upon program termination. So you shouldn't loop throught imgPaths and delete[] each member, just delete[] imgPaths.

Another possibility is that you did dynamically allocate the members of img but were deleting them elsewhere in the program. Thus when your destructor tried to delete them a second time it failed. These are the reasons that std::vector and std::string are so much better than raw arrays. Their memory is automatically managed for you, so you don't have to worry about it.

Enigma
Thanks alot Enigma. It was exactly what I thougt it was. So actually I'm only refering to the "img" witht "imgPath", so I don't have to wory about deleting it. Thanks again!

Quote:Original post by Enigma
Although snk_kid has pointed out the correct way of doing things, I thought I'd try to help you understand why your current version is not working (assuming you're code is as in your other thread, not this one). I assume you have something like:
char* img[n] = {"a string", "another string", ...};

or
char** img = new char*[n];image[0] = "a string";image[1] = "another string";...

In either case, when you call imgPaths = img you are copying a char* that was not dynamically allocated with a call to new[]. If it wasn't allocated by new[] it shouldn't be deleted with delete[]. In this case the strings are stored in the executable image. You do not need to manage them at all, they will be automatically unloaded with the rest of the executable upon program termination. So you shouldn't loop throught imgPaths and delete[] each member, just delete[] imgPaths.

Another possibility is that you did dynamically allocate the members of img but were deleting them elsewhere in the program. Thus when your destructor tried to delete them a second time it failed. These are the reasons that std::vector and std::string are so much better than raw arrays. Their memory is automatically managed for you, so you don't have to worry about it.

Enigma

This topic is closed to new replies.

Advertisement