operators new/delete in constructor/destructor

Started by
18 comments, last by kudi 21 years, 6 months ago
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 ?
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
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]
What happens when it breaks down? Also, make sure your destructor is actually example::~example, not example::example as you have.

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]
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
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.
Oh...and make sure that you have a good copy constructor and assignment operator (or disable them). As always, post if you need help with it.
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.
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)
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
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.
I''m 99% sure this object''s getting copied by value and therefore the memory it owns is being deleted twice.

This topic is closed to new replies.

Advertisement