Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kudi

operators new/delete in constructor/destructor

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

hi, in one of my classes i have a dynamic array: class example { private: char* data; public: example(); ~example(); } i want to initialize ''data'' in the constructor and after that delete ''data'' in the destructor. example::example() { data=new char[SIZE]; } example::example() { if(data) { delete [] data; data=NULL; } } i think that''s nothing very difficult and i hope that i haven''t made any mistake. i use this class every frame, i mean the constructor/destructor is called every frame. I use a simple DOS console. void main() { while(1) { example var; } } my problem is, everything works fine, if SIZE is small like 4, but if SIZE ist for example 4000, after some time, my program breaks down. do you have any ideas why ?

Share this post


Link to post
Share on other sites
Advertisement
[EDIT: um.....yeah. i'm retarded. no idea why it's messin up]

-me

[edited by - Palidine on October 3, 2002 6:30:05 PM]

Share this post


Link to post
Share on other sites
What happens when it breaks down? Also, make sure your destructor is actually example::~example, not example::example as you have.

Share this post


Link to post
Share on other sites
the destructor wasn't the fault, actually i wrote it correct.

in meantime i don't think it has to do with the SIZE. perhaps i really have another mistake.

the only thing i can say, that there is an error on the screen, that has to do with the file dbgheap.c or another similar one (and something like pHeap!=......).
but i can't find the file on my disk.

[edited by - kudi on October 3, 2002 6:54:37 PM]

Share this post


Link to post
Share on other sites
Quick Tips:

1. The size should not matter.
2. It is perfectly safe to delete a null pointer.
3. You need not set your pointer to null in the destructor.

Check somewhere else in your engine for the problem, and post code if you need help.

Share this post


Link to post
Share on other sites
If the memory is for some odd reason not getting allocated in the constructor, which I would highly doubt, you might be trying to delete data that''s not yours if the pointer is not initialized to NULL. Just as a precaution, set the pointer to NULL before the memory allocation and check to see if it''s still NULL after the allocation, and if it is, print out an error message.

Share this post


Link to post
Share on other sites
Thanks for the answers, I first have to try out these things. This takes some time. Because it is a runtime error, i must run the program very often to be sure that there''s no error. (but you never can be really sure)

Share this post


Link to post
Share on other sites
quote:
If the memory is for some odd reason not getting allocated in the constructor, which I would highly doubt, you might be trying to delete data that''s not yours if the pointer is not initialized to NULL.

The C++ operator new never returns an invalid pointer. If it cannot allocate the requested amount of memory for some reason, it will throw an exception in which case 1) the pointer is not assigned a value and 2) the destructor is completely irrelevant because it never gets called.

(There is a variant of new that uses nothrow and returns a null pointer if the memory cannot be allocated, but the original poster is not using it. Even so, deleting a null pointer does nothing.)

quote:
Just as a precaution, set the pointer to NULL before the memory allocation and check to see if it''s still NULL after the allocation, and if it is, print out an error message.

Even if operator new did return a null pointer - which it does not - setting the pointer to null immediately before assigning it the value of new is pointless.

Share this post


Link to post
Share on other sites
I''m 99% sure this object''s getting copied by value and therefore the memory it owns is being deleted twice.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!