?
Solved

Delete or delete[] ?

Posted on 2003-02-26
13
Medium Priority
?
257 Views
Last Modified: 2010-04-01
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
Comment
Question by:slabrie2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 7

Expert Comment

by:burcarpat
ID: 8026855
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
 

Author Comment

by:slabrie2
ID: 8027221
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
 
LVL 7

Expert Comment

by:burcarpat
ID: 8027391
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!

 
LVL 6

Expert Comment

by:Mafalda
ID: 8027496
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
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8027865
your compiler should see that its an array and automatically fix this for you, some might warn you.
0
 

Author Comment

by:slabrie2
ID: 8028244
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
 
LVL 1

Expert Comment

by:Mathematix
ID: 8028507
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
 
LVL 1

Expert Comment

by:Mathematix
ID: 8028539
I would also suggest that you check for successful allocation with something like

if(p == NULL)
  cout << "Memory not successfully allocated." << endl;
0
 
LVL 1

Expert Comment

by:Korsar_13
ID: 8028956
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
 

Author Comment

by:slabrie2
ID: 8029022
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
 
LVL 7

Accepted Solution

by:
burcarpat earned 225 total points
ID: 8029158
> "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
 

Expert Comment

by:nik2k
ID: 8029489
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
 

Author Comment

by:slabrie2
ID: 8044543
Well I gess those links explain a bit.
Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question