CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    Why does it crash?

    For example,

    char* str = "Hi, there!";
    delete str;

    I understand I shouldn't use delete str here. But I don't understand why delete str will make program crash? Thanks for your advices.

  2. #2
    Join Date
    Nov 2001
    Posts
    323

    Re: Why does it crash?

    because you have to use the new command before you delete it
    in other words, you created the string on the stack and then delete tries to delete str on the heap which doesn't exist there...
    Last edited by Zim327; January 13th, 2005 at 02:03 PM.
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  3. #3
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Why does it crash?

    I understand that. But why will that make the program crash?
    Quote Originally Posted by Zim327
    because you have to use the new command before you delete it

  4. #4
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: Why does it crash?

    Quote Originally Posted by Zim327
    in other words, you created the string on the stack and then delete tries to delete str on the heap which doesn't exist there...
    Actually, this isn't quite true for this situation. The compiler will put a string literal for your string into the const data segment of the compiled executable, so your string pointer is set to point to a variable that is not changeable. It would be like trying to change the assembly instructions of your own executable from inside that executable. To use delete you have to be pointing to something that was allocated on the heap (or the free store, for those standards purists ).
    --EJMW

  5. #5
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Why does it crash?

    Thanks for you guys' explaination! I always enjoy learning something from trivial questions. Also I have another question. Does that mean a pointer might point to stack?
    Last edited by dullboy; January 13th, 2005 at 02:29 PM.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why does it crash?

    Quote Originally Posted by dullboy
    Thanks for you guys' explaination! I always enjoy learning something from trivial questions. Also I have another question. Does that mean a pointer might point to stack?
    A pointer can pretty much point to anything - even an invalid location. If you try to dereference an invalid pointer, the program will crash.

    Arjay

  7. #7
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Why does it crash?

    How is the example in the following,
    char* str = new char[10];
    str = "hello";
    delete str;

    still the program crashed! See, I already use new to allocate the memory for str then delete str to deallocate the memory. Why did it still crash?

    Quote Originally Posted by ejmw
    Actually, this isn't quite true for this situation. The compiler will put a string literal for your string into the const data segment of the compiled executable, so your string pointer is set to point to a variable that is not changeable. It would be like trying to change the assembly instructions of your own executable from inside that executable. To use delete you have to be pointing to something that was allocated on the heap (or the free store, for those standards purists ).

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why does it crash?

    Quote Originally Posted by dullboy
    I understand that. But why will that make the program crash?
    This all depends on how the compiler's heap manager is coded. There is nothing stopping a heap manager to recognize that "str" wasn't previously allocated, so it doesn't do anything, making your app "safe". That is definitely not how the heap manager is coded for your particular version of VC++, but maybe another version of VC++ makes this check in the heap manager, or does something else to avoid (or sidestep) the crash.

    Therefore the behavior is undefined -- expect anything to happen if you attempt to use "delete" on a pointer that was not returned by "new".

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why does it crash?

    Quote Originally Posted by dullboy
    How is the example in the following,
    char* str = new char[10];
    str = "hello";
    delete str;

    still the program crashed! See, I already use new to allocate the memory for str then delete str to deallocate the memory. Why did it still crash?
    1) Wrong form of delete.

    2) You don't assign to strings the way you are doing it.
    Code:
    #include <cstring>
    int main()
    {
        char* str = new char[10];
        strcpy(str,"hello");
        delete [] str;
    }
    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Why does it crash?

    Quote Originally Posted by dullboy
    How is the example in the following,
    char* str = new char[10];
    str = "hello";
    delete str;

    still the program crashed! See, I already use new to allocate the memory for str then delete str to deallocate the memory. Why did it still crash?
    see difference between delete and delete[]

  11. #11
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Why does it crash?

    Of course, I know what you suggested is the way to do it. My question is what is wrong with my way?
    Quote Originally Posted by Paul McKenzie
    1) Wrong form of delete.

    2) You don't assign to strings the way you are doing it.
    Code:
    #include <cstring>
    int main()
    {
        char* str = new char[10];
        strcpy(str,"hello");
        delete [] str;
    }
    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: Why does it crash?

    Quote Originally Posted by dullboy
    How is the example in the following,
    char* str = new char[10];
    str = "hello";
    delete str;

    still the program crashed! See, I already use new to allocate the memory for str then delete str to deallocate the memory. Why did it still crash?
    Here is what is happening:
    Code:
     char* str = new char[10]; //a new character buffer is allocated on the heap.  
      //Let's pretend the address you get back is 0x0bb000a0 just for fun.  
      //Now the value of the 'str' variable equals 0x0bb000a0, right?
       
       str = "hello";  //OK.  The compiler had set aside a constant with the value 
      //"hello" in the data segment of your program.  
      //This might be at, say, memory address 0x010000aa.  
      //Now you're setting the value of str = 0x010000aa.  So you now have
      //nothing pointing to your allocated string that is still hanging around at 
      //0x0bb000a0!
       
       delete str;  // Now, you're trying to delete the memory at location 
      //0x010000aa!  This is in your program's const data segment and can't be
      //deleted!  Not only that, but you don't have any way to clean up the string
      //at 0x0bb000a0, either, so you'll get a memory leak to boot.
    Hope that clears things up!
    --EJMW

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why does it crash?

    Quote Originally Posted by dullboy
    My question is what is wrong with my way?
    Because the C++ language specification says it's wrong (more precisely, undefined). You don't code an application that invokes undefined behavior.

    Others may point to underlying code, but there is no need to. The C++ language spec clearly states that this is undefined behavior, regardless of what the underlying code may state. Even if it worked without crashing, you don't write code that does this.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 13th, 2005 at 03:00 PM.

  14. #14
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Why does it crash?

    Excellent! So simply say when I assigned "hello" to str, I changed the address str pointed to.
    Quote Originally Posted by ejmw
    Here is what is happening:
    Code:
     char* str = new char[10]; //a new character buffer is allocated on the heap.  
      //Let's pretend the address you get back is 0x0bb000a0 just for fun.  
      //Now the value of the 'str' variable equals 0x0bb000a0, right?
       
       str = "hello";  //OK.  The compiler had set aside a constant with the value 
      //"hello" in the data segment of your program.  
      //This might be at, say, memory address 0x010000aa.  
      //Now you're setting the value of str = 0x010000aa.  So you now have
      //nothing pointing to your allocated string that is still hanging around at 
      //0x0bb000a0!
       
       delete str;  // Now, you're trying to delete the memory at location 
      //0x010000aa!  This is in your program's const data segment and can't be
      //deleted!  Not only that, but you don't have any way to clean up the string
      //at 0x0bb000a0, either, so you'll get a memory leak to boot.
    Hope that clears things up!

  15. #15
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: Why does it crash?

    Yes, that is exactly right. That is the important thing to understand about pointers; changing their value only means that you're changing the location that they are pointing to. It doesn't mean that you are changing what is contained at that location.
    --EJMW

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured