Thread: Problem with Delete

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Problem with Delete

    Code:
    #include<iostream.h>
    #include<cmath>
    
    int main()
    
    {
     int elements;
     cin>>elements;
     unsigned long *dynar = new unsigned long[elements];
     for ( int x = 1; x <= elements; ++x )
     dynar[x-1] = (unsigned long)pow(x,2);
     delete []dynar;
     for ( int x = 1; x <= elements; ++x )
     cout<<"Element "<<x-1<<" is "<<dynar[x-1]<<"\n";
     return 0;
    }
    I was expecting the delete command to screw up the output, but it doesn't. What is it about delete that I'm not understanding?

    (using Bloodshed Dev-C++ 4.9.8.0)

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    my understanding is that you have to first delete all values in the elements of a dynamically allocated arrays...This is because the values in N numbers of elements of that array are still somewhere in memory, so you want to clean up everything, not just the array which holds it...try the following code:
    Code:
    for( int i = 0; i < elements; i++ ) {
         delete[] dynar[i];
    }
    delete[] dynar;
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Hey thanks :)

    Hey, thanks for the swift response I'll do just that!

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I disagree (rather strongly) with axon.

    Do not delete the elements inside dynar because you did not create them with new. Only delete things you created with new, and only delete [] things you created with new [].

    I believe this only works by coincidence. It is undefined behavior to access elements of the array that has been deleted, which means anything can happen, including, if you are lucky (or unlucky actually), it working.

    On any given implementation or at any given time code like this could crash the program, but that doesn't mean it always will.

    By the way, on my compiler it gives garbage output.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Could you do me a favor?

    Can you omit the delete statement and see if it still outputs garbage?

    On my compiler it outputs exactly what it should.

    The delete statement should, normally, free the memory, right? To me, all that matters is that I can rely on memory being freed up when I delete the array pointer.

    By the way, when do I go past cache and start to hit actual Random Access Memory?

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If I remove the delete [] it does output correct values (or at least not garbage). Of course, then there is a memory leak. If you just move the delete [] until after the output it prints out correctly and frees the memory correctly.

    Basically you are using delete [] correctly. You can't really tell that it is being freed by attempting to access it afterwards, since that behavior is undefined and might work. You just have to rely on your knowledge that you are using delete correctly.

  7. #7
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Yes, jlou is right, the memory will stay as before (even freed) until the system allocate it for something else.

  8. #8
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    That's why it is possible for police etc. to recover data from a formatted Hard Drive. When you delete something you don't delete it, you just signal that it's OK to overwrite it.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Problem with Delete

    Originally posted by Grins2Pain
    Code:
    #include<iostream.h>
    #include<cmath>
    
    int main()
    
    {
     int elements;
     cin>>elements;
     unsigned long *dynar = new unsigned long[elements];
     for ( int x = 1; x <= elements; ++x )
     dynar[x-1] = (unsigned long)pow(x,2);
     delete []dynar;
     for ( int x = 1; x <= elements; ++x )
     cout<<"Element "<<x-1<<" is "<<dynar[x-1]<<"\n";
     return 0;
    }
    I was expecting the delete command to screw up the output, but it doesn't. What is it about delete that I'm not understanding?

    (using Bloodshed Dev-C++ 4.9.8.0)
    As people have said, when you delete[], you are only telling the OS that it is OK to reuse that memory location. Until something actually DOES reuse that memory location, the old data will still be there.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. MSVC 2003 Debugging delete
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 09-09-2007, 12:15 PM
  3. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  4. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  5. Problem with Tutorial #2
    By Keiyentai in forum Windows Programming
    Replies: 2
    Last Post: 03-20-2005, 06:38 PM