Jump to content
  • Advertisement
Sign in to follow this  
PCosmin89

[ Help ] Heap error due to delete []

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

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 Palidine
But really you should be using std::string since you're using C++

Quote:
Original post by _fastcall
Also, 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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!