-1

Statement:

Memory allocated with new int[10] must be deallocated with delete[].

So..

new int[10];

Then how do I delete it?

EDIT: Thank you guys :) I think Wilson, Paul, Tim etc. are right, the statement omit some words therefore confused me - -...it's simply

int * p = new int[10];

delete [] p; 

I've never seen Rob's answer though, looks really new to me! Any explanation will be appreciated.:)

delete[] new int[10];
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Arch1tect
  • 4,128
  • 10
  • 48
  • 69

3 Answers3

6

You're missing a variable to hold the value returned from new. You should have something like:

int* intArray = new int[10];

then later when you want to delete the memory, you reference it through the variable:

delete[] intArray;
1

Just like it says:

int *foo = new int[10];
delete[] foo;
Tim
  • 59,527
  • 19
  • 156
  • 165
  • if I will not delete any memory and program exits. Then there could be any risk? ( Mean we just need to delete memory on run time? ) – Asif Mushtaq Aug 02 '15 at 20:34
1
int * p = new int[10]; // allocate p

// do stuff with p

delete [] p;           // release p when you're done
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • if I will not delete any memory and program exits. Then there could be any risk? Someone told me that if we will not deallocate the memory and program exit, then that memory will unusable until operator system reinstalled. – Asif Mushtaq Aug 02 '15 at 20:35
  • Most modern operating systems will clean up remaining memory allocations on program exit, but it's a good idea to free all dynamic allocations anyway - it makes it easier to find memory leaks and it may become important if the program is re-architected. – Paul R Aug 02 '15 at 21:47
  • What do you mean when you say "clear/clean the memory" ? – Paul R Aug 03 '15 at 05:02