Jump to content
  • Advertisement
Sign in to follow this  
CoB_ChrizZz

delete kills my operator=

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

....and again, hi !

this time my "[color=#0000DD][font=Consolas, Menlo, Monaco,]delete[/font][color=#000000][font=Consolas, Menlo, Monaco,] [/font][color=#008000][font=Consolas, Menlo, Monaco,][[/font][color=#008000][font=Consolas, Menlo, Monaco,]][/font][color=#000000][font=Consolas, Menlo, Monaco,] name[/font][color=#008080][font=Consolas, Menlo, Monaco,];" in row 73 is killing my output: "[/font][color=#0000DD][font=Consolas, Menlo, Monaco,]cout[/font][color=#000000][font=Consolas, Menlo, Monaco,] [/font][color=#000080][font=Consolas, Menlo, Monaco,]<<[/font][color=#000000][font=Consolas, Menlo, Monaco,] st3[/font][color=#008080][font=Consolas, Menlo, Monaco,];" in row 98.[/font]
[color=#008080][font=Consolas, Menlo, Monaco,]everything should be recreated in strdubnew();[/font]
[color=#008080][font=Consolas, Menlo, Monaco,]if i outcomment the delete however everything "works" as supposed.[/font]


http://pastebin.com/mjeb1qFP

Share this post


Link to post
Share on other sites
Advertisement
In your constructor, name = "" makes it point to memory that is NOT heap-allocated (with new[]). It points probably to static memory. And you should NEVER delete this memory. You should strdupnew your empty string too.

Share this post


Link to post
Share on other sites
When str3 is constructed, the default constructor is called and name gets set to the address of the string "". Yes, "" does have an address, and deleting it is invalid. You should probably set name to NULL, or allocate 1 char and set it to '\0'.

[edit]

ninja'd!

Share this post


Link to post
Share on other sites
Line 61 will also cause you problems:

int main() {
Student a( "Fred", 0 );
Student b( a ); // a.name and b.name point to the same string
} // a.name is deleted twice


EDIT:
Derp. Edited by fastcall22

Share this post


Link to post
Share on other sites

Line 61 will also cause you problems:

int main() {
Student a( "Fred", 0 );
Student b = a; // a.name and b.name point to the same string ("Fred" leaked!)
} // a.name is deleted twice



Fixed that for you :)

Share this post


Link to post
Share on other sites
The default constructor is broken, as others have pointed out.

The constructor that takes a const char * and an unsigned long is broken. because it doesn't allocate an extra char for the terminating '\0' (so the strcpy writes it beyond the allocated block).

The copy constructor is also broken, because it doesn't delete the old name and because it makes a shallow copy of the new name, which means that double deletion will happen when both objects are destroyed.

Share this post


Link to post
Share on other sites

The copy constructor is also broken, because it doesn't delete the old name...


Maybe I'm just really rusty on my C++, but why would the copy constructor need to delete the old name?

Share this post


Link to post
Share on other sites

[quote name='alvaro' timestamp='1327987456' post='4907885']
The copy constructor is also broken, because it doesn't delete the old name...


Maybe I'm just really rusty on my C++, but why would the copy constructor need to delete the old name?
[/quote]

Otherwise nobody will release the corresponding memory. See the code above that says ("Sally" leaked). It really is "Sally", not "Fred" that is leaked.

Share this post


Link to post
Share on other sites

[quote name='Cornstalks' timestamp='1327987877' post='4907887']
[quote name='alvaro' timestamp='1327987456' post='4907885']
The copy constructor is also broken, because it doesn't delete the old name...


Maybe I'm just really rusty on my C++, but why would the copy constructor need to delete the old name?
[/quote]

Otherwise nobody will release the corresponding memory. See the code above that says ("Sally" leaked). It really is "Sally", not "Fred" that is leaked.
[/quote]

Why is the assignment operator not called and the copy constructor is in that case? Both a and b have been fully constructed. I could've sworn the copy constructor is only called when you are both creating and assigning to a variable, and not when you create it and then later assign to it.

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!