Sign in to follow this  

deleting char**

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = new char[some_size];
}


Then you have to do:

for (int i=0; i<FACE_COUNT; i++) {
delete [] imgPaths[i];
}
delete [] imgPaths;


HTH,

Share this post


Link to post
Share on other sites
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[i] = new char[some_size];
}


Then you have to do:

for (int i=0; i<FACE_COUNT; i++) {
delete [] imgPaths[i];
}
delete [] imgPaths;


HTH,


It does not help, I get runtime error saying "Debug Assertion Failed..."

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i] = img[i] 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

Share this post


Link to post
Share on other sites
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[i] = img[i] 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

Share this post


Link to post
Share on other sites

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this