delete is not deleting the value???

This is a discussion on delete is not deleting the value??? within the C Programming forums, part of the General Programming Boards category; why is it happening so??? Code: void main() { char *x="hello"; printf("%s \n",x); delete x; printf("%s \n",x); getch(); } .....why ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    delete is not deleting the value???

    why is it happening so???
    Code:
    void main()
    {
    char *x="hello";
    printf("%s \n",x);
    delete x;
    printf("%s \n",x);
    getch();
    }
    .....why does it print "hello" the second time also???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    It could do anything, because the code is broken in so many ways.

    1. main returns int, not void
    SourceForge.net: Void main - cpwiki

    2. delete is a C++ thing, but the rest of the code is apparently C

    3. You can only delete/free something which was originally allocated using new/malloc

    4. Even when you've fixed all those problems, deallocating memory does NOT automatically make your pointer NULL, nor does it necessarily mean that the memory it points to is 'reset' in some way. If the code dereferences a pointer after calling free, then it is broken - no matter how much it seems to you like nothing has happened.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Then what is the use of delete or free when we can retrieve the value after using them???

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by mohnish_khiani
    Then what is the use of delete or free when we can retrieve the value after using them???
    To destroy the object or to free memory. Note that attempting to "retrieve the value after using them" results in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mohnish_khiani View Post
    Then what is the use of delete or free when we can retrieve the value after using them???
    To expand a little on what laserlight (Hi Lase!) said...

    When you use malloc() to reserve memory for an object, you have to free() the object when done with it...

    However, free() does not *erase* the data. It only releases the memory used back to the shared memory pool. The values you stored in that memory are not erased... but they might be overwritten at any instant by a malloc() call from elsewhere. Thus you cannot trust memory that has been freed, resulting in unpredictable (undefined) behavior.

    The reason that memory is not erased is a simple matter of speed. Going through a large chunk of memory and resetting it all to 0 (or some other value) takes time. If it's done often enough it will impact on your software's performance and the performance of the machine itself. So they just leave it sitting there until the space is needed for something else.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    > Then what is the use of delete or free when we can retrieve the value after using them???
    Try this
    Code:
    int main ( ) {
        char *bar;
        char *foo = malloc(10);
        strcpy( foo, "hello" );
        printf("foo=%s\n", foo );
        free( foo );
    
        bar = malloc(10);
        strcpy( bar, "world" );
        printf("foo=%s\n", foo );  // What does this print, and WHY
        printf("bar=%s\n", bar );
        free( bar );
        return 0;
    }
    Now on machines which allocate the same block of memory that was just freed (if it is large enough), foo and bar will end up pointing to the same place.

    Now imagine if you had instead of just printing foo after deleting it, you had done something dangerous like
    strcpy( foo, "bombed" );
    How disappointed would you be to see on trying to print bar that you didn't get the world you were expecting?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    yaa it prints world the second time....thus resulting in unpredictable behaviour...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 02:52 PM
  3. updating and deleting a record
    By Burritt in forum C Programming
    Replies: 2
    Last Post: 04-19-2003, 05:58 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21