delete char * - why does it work

Discussion in 'C++' started by puzzlecracker, Jul 16, 2005.

  1. interesting case:

    class MyString{
    char * strRep; // initialized to char array

    public:
    ~MyString(){delete strRep;} //why would this work
    // just like 'delete [] strRep;'
    };


    isn't delete first calls the destructor for the object and then
    deallocates the memory (by operator delete)?
     
    puzzlecracker, Jul 16, 2005
    #1
    1. Advertising

  2. Re: delete char * - why does it work

    puzzlecracker wrote:
    > interesting case:
    >
    > class MyString{
    > char * strRep; // initialized to char array
    >
    > public:
    > ~MyString(){delete strRep;} //why would this work
    > // just like 'delete [] strRep;'


    It doesn't if strRep was allocated with new[]. It does if strRep was
    allocated with new.

    char *c = new char[10];
    delete c; // error

    char *c = new char;
    delete c; // ok

    That's undefined behavior. On my system, mismatched new and delete
    crashes almost everytime.


    Jonatha
     
    Jonathan Mcdougall, Jul 16, 2005
    #2
    1. Advertising

  3. Re: delete char * - why does it work

    let's say it is allocated as char *c ="dummy";

    IT WILL WORK!
     
    puzzlecracker, Jul 16, 2005
    #3
  4. Re: delete char * - why does it work

    puzzlecracker wrote:
    > let's say it is allocated as char *c ="dummy";
    >
    > IT WILL WORK!


    Please quote what you are answering to next time.

    Depends on what you mean by 'it will work'. If you intend to crash your
    system, it may or may not work. If you intend to run into undefined
    behavior, it will work. If you intend to have a well-behaving program,
    it won't. Deleting something you haven't allocated yourself is illegal.


    Jonathan
     
    Jonathan Mcdougall, Jul 16, 2005
    #4
  5. Re: delete char * - why does it work

    "puzzlecracker" <> wrote in message
    news:...
    > let's say it is allocated as char *c ="dummy";
    >
    > IT WILL WORK!



    It is undefined behaviour, looking as if it worked.
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 16, 2005
    #5
  6. Re: delete char * - why does it work

    the behavior is undefined - but why does it work on all compilers?????
     
    puzzlecracker, Jul 16, 2005
    #6
  7. puzzlecracker

    Markus Moll Guest

    Re: delete char * - why does it work

    Hi

    puzzlecracker wrote:

    > the behavior is undefined - but why does it work on all compilers?????


    Come on... don't answer your own questions.


    Markus
     
    Markus Moll, Jul 16, 2005
    #7
  8. puzzlecracker

    John Carson Guest

    Re: delete char * - why does it work

    "puzzlecracker" <> wrote in message
    news:
    > the behavior is undefined - but why does it work on all compilers?????


    I doubt that you have tested all compilers.

    You really need to get over this nonsense. Undefined means that anything can
    happen. Included in the "anything" is that it might work as you expect.
    There is no explanation in standard C++ as to why something with undefined
    behaviour might "work". That all depends on the details of compiler
    implementations etc. If you want to know, then go ask the authors of the
    compiler.

    If you write correct code, you are entitled to expect it to work. If you
    write code that invokes undefined behaviour, you have no right to expect
    anything. The fact that code with undefined behaviour sometimes works in the
    way expected by the programmers who write it is neither new nor interesting
    information. Programmers who rely on such "good fortune" are fools.

    --
    John Carson
     
    John Carson, Jul 16, 2005
    #8
  9. Re: delete char * - why does it work

    "puzzlecracker" <> wrote in message
    news:...
    > the behavior is undefined - but why does it work on all compilers?????



    It does not. Proof? Try VS 7.1 on Win XP SP2 with:

    class MyString
    {
    char* strRep;

    public:
    MyString (char* t) : strRep (t)
    {
    }

    ~MyString ()
    {
    delete strRep;
    }
    };

    MyString s = "test";


    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 17, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    504
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,258
    Smokey Grindel
    Dec 2, 2006
  3. D. Susman

    Why does "delete" not delete?

    D. Susman, Mar 10, 2008, in forum: C++
    Replies:
    11
    Views:
    548
    Andre Kostur
    Mar 11, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    612
    PerlFAQ Server
    Feb 11, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    601
    PerlFAQ Server
    Mar 9, 2011
Loading...

Share This Page