1

I am having a problem with freeing array of strings. I have a program but I made this simple code to see the error:

char *cardsName[2];
cardsName[0] = new char[3];
cardsName[0] = "a";
cardsName[1] = new char[3];
cardsName[1] = "c";
for(int i=0;i<2;i++)
    delete(cardsName[i]);

This giving me error munmap_chunk(): invalid pointer: 0x080489b8 ***

and this also giving me error:

char *cardsName[2];
cardsName[0] = new char[3];
cardsName[0] = "a";
cardsName[1] = new char[3];
cardsName[1] = "c";
delete []cardsName;

free(): invalid pointer: 0xbfc38dd8 ***

Then how I do free for array of pointers to char,... I really need it for my program and I really searched a lot and I can't find solution for this simple problem.

9
  • 2
    Your C++ code looks a lot like C code. Have you considered using standard library classes, e.g. std::array and std::string? Commented Mar 18, 2015 at 16:27
  • i have a homework in c++ (I just started the course) in the hw its not allowed to use STL classes etc.. and this is just part of the code without the includes Commented Mar 18, 2015 at 16:29
  • 2
    Strange how many C++ courses disallow the best parts of C++... Commented Mar 18, 2015 at 16:30
  • Not allowed at this part to use string at all (xD) first week of the course is programmed to give work with array of pointers etc.. Commented Mar 18, 2015 at 16:34
  • 1
    @Saif " ... first week of the course is programmed to give work with array of pointers ..." This should be done in the courses advanced sections, not for beginners. Sigh! Commented Mar 18, 2015 at 16:37

2 Answers 2

1

The statement

cardsName[0] = "a";

assigns a pointer to the string literal "a", which can't be deleted. The pointer you have allocated with cardsName[0] = new char[3]; is discarded, and you will suffer from a memory leak.

You probably meant to use strncpy() to assign the value:

strncpy("a",cardsname[0],3);

The second problem is, that

delete []cardsName; 

is also wrong. You never allocated this array with new[] thus the statement fails.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes i ment strcpy probably
@Saif Extended in my answer.
I didn't get why cardsName[0] = "a"; cant be deleted, but with strcpy it worked but can you explain why? thanks
@Saif "a" is a char* pointer itself, that points to an object with static storage duration (the string literal), thus it can't be deleted. The strcpy() copies the character values from the string literal, to your pointer that was formerly created with new char[3];.
@ πάντα ῥεῖ ok i got it, but if i have int *arr[]; what whould be the soultion?, because then i don't have strcpy or function to copy them
|
0

This isn't an array of string, because you only use char* cardsName - which is a string(array of chars). If you want to make an array of string, you need to use array of arrays of chars - char** cardsName.

Also, instead using naked pointers and char arrays you should use std::string and std::vector/std::array.

4 Comments

yes array of char pointers, string would be more right but its not allowed to be done in this question
@Fireho To cite from OP's comments: "in the hw its not allowed to use STL classes etc.. ..."
@πάνταῥεῖ That was a suggestion for future. I provided a right answer about this case anyway.
@Fireho Right answer for what? Why the deletion fails, or is it just a nitpick about calling a char* a string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.