Jump to content
  • Advertisement
Sign in to follow this  
jarod83

deleting char**

This topic is 5113 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

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...

Share this post


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

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 = 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..."

Share this post


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

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 = 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

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 = 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!