delete array of pointers

This is a discussion on delete array of pointers within the C++ Programming forums, part of the General Programming Boards category; hi, i have declared and initialized an array of pointers like this: int* tmpArray=new int[size]; whatz the difference between: delete[] ...

  1. #1
    Unregistered
    Guest

    delete array of pointers

    hi,
    i have declared and initialized an array of pointers like this:
    int* tmpArray=new int[size];

    whatz the difference between:
    delete[] tempArray;
    and
    delete tempArray;

    when and where do i need the brackets? and whatz the diff?
    the compiler accepts both things.

    btw, i use borland builder 3.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Probably none if you're just deleting pointers to built in types rather than user defined classes, as most delete implementations will call the standard c function free(). However to be safe you should call delete [] whenever you allocate an array rather than a single object, and you must call delete [] for arrays of user defined types as it will call all of the destructors.
    zen

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The best way to think about this is...

    if you used new then free with delete

    if you used new[] then free with delete[]

    if you used malloc() then free with free()

    remember this and you will be fine.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Eh...

    Originally posted by Unregistered

    int* tmpArray=new int[size];
    Is this really an array of pointers? I thought it was something like this:
    Code:
    
    int* Array[20];
    for(int i=0; i<20; i++)
    {
      Array[i]=new int[size];
    }
    
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

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