Re: help: class compile error

From: xuatla (xuatla_at_gmail.com)
Date: 07/10/04


Date: Fri, 09 Jul 2004 19:28:29 -0700

Thanks a lot. It's clear for me now.

X

Howard wrote:
> "xuatla" <xuatla@gmail.com> wrote in message
> news:40EF066E.90801@gmail.com...
>
>
>>One more question about new and delete:
>>
>>I use new to create a CTest. And then delete it. But it seems that the
>>deletion is not successful. When I print it, it's still there.
>>
>>Do I need to define new and delete for class by myself?
>>
>>// main function
>>int main(void)
>>{
>>int N = 10;
>>CTest t1(N);
>>int i;
>>
>>for ( i = 0; i < N; i++ ) t1[i] = i;
>>
>>CTest *t2 = new CTest;
>>
>>*t2 = t1;
>>
>>for ( i = 0; i < N; i++ ) cout << (t1+*t2)[i] << endl;
>>
>>delete t2;
>>
>>for ( i = 0; i < N; i++ ) cout << (*t2)[i] << endl;
>>// still print out t2.
>>
>>return 1;
>>}
>>
>>Thanks a lot for your kind help!
>>
>>X
>
>
> Once you call delete on the pointer t2, it is illegal to use that pointer
> (unless you assign it to another valid object first). What you are doing
> *may* work in some cases (because it is *possible* that the memory at that
> location has not changed...yet). But you can *never* rely on doing that
> successfully. You are invoking "undefined behavior", which means that
> anything can happen. It may work, it may crash, and it may even cause
> *serious* problems! Simply put, "don't do that!" :-)
>
> By the way, calling delete does not cause the computer to go to that memory
> location and erase anything. What it does (well, one of the things it does,
> somehow, behind the scenes), is mark the memory as "invalid" that was
> previously used by the object that was destroyed. At any time in the
> future, the computer is free to use that memory for any other purpose it
> needs it for. In this simple case, it just happened that that memory was
> still the same as when you called delete, so nothing bad happened when you
> used it (illegally). But try it again sometime and you may have to reboot
> your computer...or worse!
>
> -Howard
>
>
>
>
>



Relevant Pages

  • Re: help: class compile error
    ... > I use new to create a CTest. ... > deletion is not successful. ... (unless you assign it to another valid object first). ... calling delete does not cause the computer to go to that memory ...
    (comp.lang.cpp)
  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: "Mastering C Pointers"....
    ... all means go ahead and dive right into the C language. ... Memory is a separate unit which just stores bits. ... A pointer at the hardware level _is an integer_. ... since loops make your logic more much ...
    (comp.lang.c)