• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Delete or delete[] ?

I just don't understand??

I code:

class A
{
public:
  A(){
    ptrInt = new int[1000];
    for(int i=0;i<1000;i++) ptrInt[i] = i;
  }
  ~A(){
    //delete[] ptrInt;
    delete ptrInt;
  }
private:
  int * ptrInt;
}

This is an example of a class that I create & delete many times to test the memory... Funny thing is both (delete and delete[]) clear all the memory!!! I was expecting a lack of memory by using 'delete' on a array pointer!

Is someone can explain me that?

By the way, I'm using the LINUX /proc/[# of my proc]/status to look at the memory. There's many information like RSS, Size, Data, ... I can't find any description of those information. I use RSS to watch the memory. Can you tell me what mean all those information... like Data.

Thanks!
0
slabrie2
Asked:
slabrie2
  • 4
  • 3
  • 2
  • +4
1 Solution
 
burcarpatCommented:
it's delete[] but i'd suggest using std::vector instead.  modern oses can cleanup the memory after the program ends; that's why you are not getting any leak
0
 
slabrie2Author Commented:
But I don't end the program, I look at the memory while he's waiting for a key (cin).

Do you think the compilator know the difference between an array pointer and a simple pointer when I call delete?

std::vector? In fact I use "#define zap_(x) {assert(x != NULL); delete[](x); x=NULL; }". Is std::vector better?

Do you have an answer about those info in LINUX /proc/[# of my proc]/status ?

Thanks!
0
 
burcarpatCommented:
hmmm, well, i believe delete <insert_array_pointer> is actually undefined ( need to check the standard, though ) so may be your compiler ( gcc, i presume ), is taking care of that automatically.  yet, even if that was true i don't think that would be a portable solution

about std::vector...  yes, std::vector is 99% of the time better than a dynamic array and should always be the first choice unless you have a very good reason not to do so

furthermore, "delete p;" won't give an error even if p is NULL ( surprising but that's how it is ) and i believe "delete[] p;" is no different ( i need to check that out too to be sure.  i am sure about delete but not delete[] )

sorry, i am not writing from my computer and thus i don't have the pdf of the standard nearby or access to gcc, that's why i keep saying "i need to check that"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MafaldaCommented:
Your compiler might have optimized the binary code in a way that in your specific case you "see" that the memory is released.
Try using a more complex array instead of type int.
I think that if you use a little more complex object you will soon enough see the memory leak (memory unreleased).
You can start with a double ...
0
 
PlanetCppCommented:
your compiler should see that its an array and automatically fix this for you, some might warn you.
0
 
slabrie2Author Commented:
In my case Burcarpat, using zap() instead of delete give me an error if NULL.

I try with a struct (size=20) and the same thing append! No difference between delete & delete[].

I look at all warnings from my linux compiler and there's nothing about any fix of the 'delete' line.

That is true Burcarpat, next time I will use std::vector.

Now, I still can't figure what's going on! If I revove delete, the RSS of LINUX /proc/[# of my proc]/status jump up to 40 000K. With delete or delete[], both do the job releasing the memory.

Any answer about ...[proc]/status information?
0
 
MathematixCommented:
Okay, let's look at the difference between delete p and delete [] p.

If you have a pointer variable...

char *p;

and you allocate it with

p = new char;

then you would delete it with

delete p;

Now, if you have the same declaration for p as before, but you allocate it with

p = new char[100];

then you will need to delete it by doing

delete [] p;

because p is a pointer to a character array rather than a pointer to a single character.
0
 
MathematixCommented:
I would also suggest that you check for successful allocation with something like

if(p == NULL)
  cout << "Memory not successfully allocated." << endl;
0
 
Korsar_13Commented:
IIRC, delete[] p; calls destructors for EACH allocated object and then call something like delete p;
delete p; calls destructor only for first object in array

Check this code for array of objects with allocating memory in cosntructor and deallocating in destructor and see difference

0
 
slabrie2Author Commented:
Sorry Mathematix but I already use delete the good way using : "#define zap_(x) {assert(x != NULL); delete[](x); x=NULL; }".

Now I just question myself about the way I use to look at the memory used by the program!

I was expecting the Linux VmRSS found in /proc/[#]/status to be the memory size used by the program but now I'm not sure!

Using the calculator, I just realize that the call 'ps -al' look more significan compare to VmRSS.

Anyway, what I see with both 'delete' still the same!

Is someone know the best way to:
1-Get the total memory size of the process
AND
2-The memory size used (when we allocate memory)

Thanks!!
0
 
burcarpatCommented:
> "I try with a struct (size=20) and the same thing append! No difference between delete & delete[]."

than your compiler is not standard conforming.  please see

  http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.7

and also the following faq, 16.8 for this.  also, see the c++ standard item 3.7.3.2, paragraph 3.  this clearly explains that giving a null pointer to either delete or delete[] should not cause any errors


> "Now, I still can't figure what's going on!"

i believe your compiler is taking control here 'cause, as i suspected, the behavior is actually undefined as per the same above item of the standard, paragraph 4

also, see,

    http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11

and the following faq 16.12 for a discussion of the topic.  your current compiler might be doing the clean-up for you but you should not depend on it

( btw, this faq is the official faq of comp.lang.c++.moderated, which is the newsgroup many famous c++ gurus hang out and thus can be considered almost as a definitive authority for all practical purposses )
0
 
nik2kCommented:
Actually deleting arrays with a simple delete should be ok in most compilers, when deleting a type without destructor.
All delete [] does, is it calls descructor on each element and then frees the memory.  Well there is not much to call for type int.  If it was an array of some type with destructor, then destcructors for each member wouldn't be called.
0
 
slabrie2Author Commented:
Well I gess those links explain a bit.
Thanks
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now