Public Group

[ Help ] Heap error due to delete []

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

Recommended Posts

Greetings, I would greatly appreciate it if you guys could help me find the fault in my logic, I have been looking at this piece of code for some time now and I can't figure out what I'm doing wrong. I believe this is the code needed to see what is wrong:
// Default constructor for a carte class
carte::carte()
{
titlu = new char[10];
titlu = "NeInit";

autor = new char[10];
autor = "NeInit";

pret = 0;
}

// Function to set all parameters ( for loop declarations )
void carte::set_all()
{

// Deleting these so I won't have any problem overwriting.
// I tried without the deletes and it worked but I will
// probably come across some problems later on
delete [] titlu; // This is where the program breaks
delete [] autor; // I presume it would break here too

char *t = new char[31];
char *a = new char[31];

cout << "Titlu: "; cin.getline(t, 30);
cout << "Autor: "; cin.getline(a, 30);
cout << "Pret: "; cin >> pret;

titlu = new char[strlen(t)+1];
autor = new char[strlen(a)+1];

strcpy(titlu, t);
strcpy(autor, a);

delete [] t;
delete [] a;
}


In the main body of the function, I have something like:
carte test;
test.set_all(); // This is pretty much where it breaks


Also, if anyone has the time/mood to look ( perhaps I'm wrong and the problem is elsewhere ), you can find the ( almost :P ) complete code here. Thanks, Parvulescu Cosmin

Share on other sites
titlu = new char[10];titlu = "NeInit";

That doesn't copy the string "NeInit" into the new memory. It assigns the pointer titlu to point to the beginning of where "NeInit" appears in memory. So you're then trying to delete[] memory that wasn't allocated on the string.

If you really want to use c-style strings you have to use strcpy to actually copy the data "NeInit" into the newly allocated memory:

titlu = new char[10];strcpy(titlu,"NeInit");

But really you should be using std::string since you're using C++

EDIT:
and I mean, since you have the strings declared statically anyway, there's really no point allocating more memory to hold them. Unless at some point you're actually modifying each instance's value for those strings, you should have them as file or class level statics.

-me

Share on other sites

Also, if you want to do anything with strings, do as older1s says and use std::string.

Share on other sites
Thank you for the fast replies guys, barely had time to take a shower :)
I see my mistake now, thanks Palidine for explaining!

Quote:
 Original post by PalidineBut really you should be using std::string since you're using C++

Quote:
 Original post by _fastcallAlso, if you want to do anything with strings, do as older1s says and use std::string.

Thank you for your inputs, I use strings in what I do for myself, unfortunately this is for a school project in which char* is supposed to be used, thus I am stuck with this for now.

Thanks again

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• Forum Statistics

• Total Topics
634082
• Total Posts
3015401
×