Thread: new/delete

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

    new/delete

    Hello, I had a question about what the difference is between delete and delete[]. Lets say for example I have this code:

    Code:
    char *tmp;
    tmp = new char[100];
    // ....
    delete char;
    Whats the difference between that, and this:

    Code:
    char *tmp;
    tmp = new char[100];
    // ....
    delete []char;
    Will they both free up the memory allocated to tmp? They both seem to compile and run without errors, just wondering if I'm causing a memory leak somewhere.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Will they both free up the memory allocated to tmp?
    No, delete [] will free all of the memory allocated to tmp, but delete will only free the memory for the first element. So yes, one would most certainly cause a memory leak.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Is this valid then?
    Code:
    int* MyPointer;
    MyPointer = new int;
    ...
    delete[] MyPointer;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is this valid then?
    No, and technically neither is delete on an array allocated by new[]. What happens when you use the wrong delete is dependent on the implementation of the operators. Both are wrong, but the usual reaction to using delete on an array is that the first element is freed but the remaining memory is not. Of course, you can't rely on this.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >but the usual reaction to using delete on an array is that the first element is freed but the remaining memory is not.<

    Actually, it doesn't normally have any affect on the immediate de-allocation. Most delete implementations will end up calling free() or something similar which don't care if one or many elements have been allocated; they'll just free a block (as you're no doubt aware).

    The problem is that in most implementations using delete on something new[]'d will result in only one destructor being called (instead of many) which could result in a secondary memory leak if you need to de-allocate in your destructor or other resource problems. Because primitives don't have destructors you *may* be able to get away with it; but as you point out, there's nothing in the standard that allows you to rely on this, so it's probably not a good idea.
    Joe

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    --Under Microsoft Visual Studio 6.0--

    Using Windows memory leak checking ( i.e. crtdbg.h ) it will NOT detect a memory leak given the following code..

    Code:
    int main( void )
    {
      int *p = new int[500];
    
      delete p;
    
      return 0;
    }
    This is strange but what isn't using VS.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >Using Windows memory leak checking ( i.e. crtdbg.h ) it will NOT detect a memory leak given the following code..<

    That's because there probably isn't a memory leak. Its still not a good idea if you want to ensure portability. What if MSVC++ 25.0 on Win XX uses completely different memory allocation routines?
    Joe

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    By the way I knwo that the pointer will store the address of the first element of the array when we say:

    Code:
    int *p = new int[100];
    So if we used this:
    Code:
    delete p;
    it will only deallocate the first element so what will happen to the rest?

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Theoretically, it just sits there wasting space until the program exits, at which point the OS will (hopefully) reclaim the memory...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Sebastiani
    Theoretically, it just sits there wasting space until the program exits, at which point the OS will (hopefully) reclaim the memory...
    I thought the lack of this was why Windows usually starts lagging if you let it run for a long time... .

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I thought the lack of this was why Windows usually starts lagging if you let it run for a long time...
    Windows does reclaim the memory, but not right away. So if you run a memory leak intensive program the computer will run slowly for several minutes.

    -Prelude
    My best code is written with the delete key.

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Prelude
    >I thought the lack of this was why Windows usually starts lagging if you let it run for a long time...
    Windows does reclaim the memory, but not right away. So if you run a memory leak intensive program the computer will run slowly for several minutes.

    -Prelude
    That's right....it does reclaim but not immeadiately....

    do something like;

    for(;;)new double[1000];

    and catch the bad_alloc exception......your PC will be pretty slow for a few minutes.....on XP, there's an emergency utility that launches and tries to set things safe...but that's not instant.....

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    This seems to work, but i'm not sure.
    Code:
    int main()
    {
    	int *myInt = new int;
    
    	*myInt = 5;
    
    	delete [] myInt;
    	
    	return 0;
    }
    Anyway if it does work then why not always use the "[]" syntax for delete?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Anyway if it does work then why not always use the "[]" syntax for delete?
    Because it's wrong not to match the correct new with the correct delete. It may work for you, but that's not guaranteed, especially if you port to another compiler or system.

    -Prelude
    My best code is written with the delete key.

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by The Dog
    This seems to work, but i'm not sure.
    Code:
    int main()
    {
    	int *myInt = new int;
    
    	*myInt = 5;
    
    	delete [] myInt;
    	
    	return 0;
    }
    Anyway if it does work then why not always use the "[]" syntax for delete?
    Because its undefined and not for what it was made for.....it may work in a small example, but it wont be trustworthy...if it was I doubt the Standards Board would have let the delete[] operator in

    Remember that delete[] expects to call multiple destructors..... you might get away with it on a built in data type, but when you are doing it with class objects, you will probably fall foul at some point...so best bet - use delete[] for new[] and delete for new

    <edit> Prelude got there first </edit>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New/delete vs malloc/free
    By KBriggs in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2009, 03:08 PM
  2. Overriding operator new/delete?
    By Elysia in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2008, 12:10 PM
  3. Replies: 6
    Last Post: 09-14-2006, 10:46 PM
  4. overloading new/delete
    By DavidP in forum C++ Programming
    Replies: 13
    Last Post: 07-02-2004, 08:17 AM
  5. new/delete problems, please help
    By btq in forum C++ Programming
    Replies: 9
    Last Post: 06-07-2002, 11:23 AM