
Error Using new & delete in class
Actually, it's always safe to delete a NULL pointer. But the point is a
good one.
Quote:
> writes
> >Need urgent assistance. when in distractor of a class I am using
> delete
> >or free to free memory that was allocated in the constructor during
> an
> >class operation, I am getting an error.
> >Does anyone have an answer to this problem? If so, please email it
> to
> >me and
> >tell me how to fix it..
> I can't really say without seeing the code or error messages, but a
> problem often occurs when trying to delete null/invalid pointers.
> It helps if you ensure the pointer's null at the start of the
> constructor by doing something like -
> pMyPointer = NULL;
> Do this before you allocate it with new.
> In your destructor, then check the pointer before deleteing, ie -
> if (pMyPointer != NULL) delete pMyPointer;
> This is guesswork, given what I know about the problem, but it just
> may
> be that you're allocation has failed, or you're reallocating somewhere
> else.
> If I delete pointers elsewhere in code, I always try to remember to
> set
> the pointer to NULL again.
> Of course it could be that the pointer's getting corrupted elsewhere,
> in
> which case it's a bit more complicated.
> --
> Gordon Ansell
> Remove '.nospam' from end of e-mail address to reply.
> Know anything about pop music? Try http:\\www.di-soft.demon.co.uk
-- Aaron
---------------------
Aaron J Margosis
ScrnSaveSwitch/Plus - Screen Saver Control Utility:
http://members.aol.com/aaronmarg/ssswitch/