Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question on delete [] vs just plain delete

Reply
Thread Tools

Question on delete [] vs just plain delete

 
 
DamonChong
Guest
Posts: n/a
 
      01-29-2005
Hi,

I am new to c++. I recently spend an enormous among of time
troubleshooting a seeminly innocuous piece of code. Although I narrow
down this piece of code as the culprit but I don't understand why. Can
some guru help to enlighten me? Thank you.

// I created an array of pointers to object pointers:

Object ** obs = new Object * [9];

// After that I populate this array:

for(int i=0; i<9; i++){
obs[i] = new Object(i);
}

// After using them i deleted the array:

for(int i=0; i<9; i++){
delete obs[i];
}

delete [] obs;

delete obs; // this is the problem, during execution the program
hanged.

Apparently, the above caused undefined behaviour. But I don't know why
i shouldn't do that. Thanks again for any sharing!

Cheers,
Damon

 
Reply With Quote
 
 
 
 
Efrat Regev
Guest
Posts: n/a
 
      01-29-2005
"DamonChong" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
>
> I am new to c++. I recently spend an enormous among of time
> troubleshooting a seeminly innocuous piece of code. Although I narrow
> down this piece of code as the culprit but I don't understand why. Can
> some guru help to enlighten me? Thank you.


While the gurus answer more difficult questions, I'll field this one.

>
> // I created an array of pointers to object pointers:
>
> Object ** obs = new Object * [9];


/////////// Great, you've allocated an array of pointers to Object

>
> // After that I populate this array:
>
> for(int i=0; i<9; i++){
> obs[i] = new Object(i);


/////////// And now you've allocated the objects.
> }
>
> // After using them i deleted the array:
>
> for(int i=0; i<9; i++){
> delete obs[i];


/////////// Now you've deallocated the objects.

> }
>
> delete [] obs;


/////////// And now you've deallocated the array.


/////////// Good. So you've deallocated everything you've allocated. Nothing
left to deallocate.

/////////// and yet....
>
> delete obs; // this is the problem, during execution the program
> hanged.


/////////// So the previous line hangs since you're deallocating something
which you shouldn't



 
Reply With Quote
 
 
 
 
Peter Koch Larsen
Guest
Posts: n/a
 
      01-29-2005
Hi Damon

Efrat already answered your post, but I will recommend you do not use
new/delete as a newbie C++ programmer. Instead focus on the standard library
and just don't use pointers.
Pointers are difficult and better not used until you understand the basics
of C++ - including the standard library. By going this route, you will also
learn how little use there actually is of pointers in modern C++.

/Peter
"DamonChong" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed) ups.com...
> Hi,
>
> I am new to c++. I recently spend an enormous among of time
> troubleshooting a seeminly innocuous piece of code. Although I narrow
> down this piece of code as the culprit but I don't understand why. Can
> some guru help to enlighten me? Thank you.
>
> // I created an array of pointers to object pointers:
>
> Object ** obs = new Object * [9];
>
> // After that I populate this array:
>
> for(int i=0; i<9; i++){
> obs[i] = new Object(i);
> }
>
> // After using them i deleted the array:
>
> for(int i=0; i<9; i++){
> delete obs[i];
> }
>
> delete [] obs;
>
> delete obs; // this is the problem, during execution the program
> hanged.
>
> Apparently, the above caused undefined behaviour. But I don't know why
> i shouldn't do that. Thanks again for any sharing!
>
> Cheers,
> Damon
>



 
Reply With Quote
 
ajk
Guest
Posts: n/a
 
      01-29-2005
On 29 Jan 2005 01:39:32 -0800, "DamonChong" <(E-Mail Removed)>
wrote:

>delete [] obs;
>
>delete obs; // this is the problem, during execution the program



normally you use delete [] on any array you allocate to tell the
compiler that the ptr you are deleting is an array.

it is just one of those not obvious c++ rules you need to learn

in any case I would suggest you use vector<> instead of array since it
is safer and more convenient. check up the online help/book for info.
STL in general can help you a lot.

/ajk
--
"Those are my principles. If you don't like them I have others."
Groucho Marx.
 
Reply With Quote
 
DamonChong
Guest
Posts: n/a
 
      01-29-2005
Thanks alot. Kind of silly I suppose to make such mistake. ;P

 
Reply With Quote
 
Jon
Guest
Posts: n/a
 
      01-29-2005
While I would agree that pointers are maybe not obvious for a beginner to
say that there is "little use" of pointers in modern c++ is just plain wrong
in my opinion. Of course it maybe depends on what you define as "modern c++"
but that is another question...

"Peter Koch Larsen" <(E-Mail Removed)> wrote in message
news:%jKKd.85876$(E-Mail Removed) ...
> Hi Damon
>
> Efrat already answered your post, but I will recommend you do not use
> new/delete as a newbie C++ programmer. Instead focus on the standard
> library and just don't use pointers.
> Pointers are difficult and better not used until you understand the basics
> of C++ - including the standard library. By going this route, you will
> also learn how little use there actually is of pointers in modern C++.
>
> /Peter
> "DamonChong" <(E-Mail Removed)> skrev i en meddelelse
> news:(E-Mail Removed) ups.com...
>> Hi,
>>
>> I am new to c++. I recently spend an enormous among of time
>> troubleshooting a seeminly innocuous piece of code. Although I narrow
>> down this piece of code as the culprit but I don't understand why. Can
>> some guru help to enlighten me? Thank you.
>>
>> // I created an array of pointers to object pointers:
>>
>> Object ** obs = new Object * [9];
>>
>> // After that I populate this array:
>>
>> for(int i=0; i<9; i++){
>> obs[i] = new Object(i);
>> }
>>
>> // After using them i deleted the array:
>>
>> for(int i=0; i<9; i++){
>> delete obs[i];
>> }
>>
>> delete [] obs;
>>
>> delete obs; // this is the problem, during execution the program
>> hanged.
>>
>> Apparently, the above caused undefined behaviour. But I don't know why
>> i shouldn't do that. Thanks again for any sharing!
>>
>> Cheers,
>> Damon
>>

>
>



 
Reply With Quote
 
Efrat Regev
Guest
Posts: n/a
 
      01-29-2005
"DamonChong" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Thanks alot. Kind of silly I suppose to make such mistake. ;P
>


No problem. BTW, explicit memory management is both very powerful, and
very prone to making "silly" mistakes (which I don't think are silly at
all). Consequently, perhaps you might want to read Peter Koch Larsen's
response (with which I completely agree).

Specifically, you could write your code this way:

#include <vector>

std::vector<Object> obs[9];

for(int i = 0; i < 9; ++i)
obs[i] = Object(i);

Or if Object doesn't have a default constructor,

#include <vector>
#include <boost/shared_ptr.hpp>

std::vector<boost::shared_ptr<Object> > obs(9);

for(int i = 0; i < 9; ++i)
obs[i].reset(new Object(i));

(You could google for boost smart_ptr for the above).


 
Reply With Quote
 
Bradley
Guest
Posts: n/a
 
      01-30-2005

My question is why dynamically allocate an array of Object pointers. Why not
just just

Object *obparray[9];

??

Is memory that sparse now days?

"DamonChong" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
>
> I am new to c++. I recently spend an enormous among of time
> troubleshooting a seeminly innocuous piece of code. Although I narrow
> down this piece of code as the culprit but I don't understand why. Can
> some guru help to enlighten me? Thank you.
>
> // I created an array of pointers to object pointers:
>
> Object ** obs = new Object * [9];
>
> // After that I populate this array:
>
> for(int i=0; i<9; i++){
> obs[i] = new Object(i);
> }
>
> // After using them i deleted the array:
>
> for(int i=0; i<9; i++){
> delete obs[i];
> }
>
> delete [] obs;
>
> delete obs; // this is the problem, during execution the program
> hanged.
>
> Apparently, the above caused undefined behaviour. But I don't know why
> i shouldn't do that. Thanks again for any sharing!
>
> Cheers,
> Damon
>



 
Reply With Quote
 
Efrat Regev
Guest
Posts: n/a
 
      01-30-2005
"Bradley" <(E-Mail Removed)> wrote in message
news:flWKd.230262$(E-Mail Removed)-kc.rr.com...
>
> My question is why dynamically allocate an array of Object pointers. Why

not
> just just
>
> Object *obparray[9];
>
> ??
>
> Is memory that sparse now days?
>


It's possible that Object doesn't have a default constructor.


 
Reply With Quote
 
Efrat Regev
Guest
Posts: n/a
 
      01-30-2005

"Efrat Regev" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> "Bradley" <(E-Mail Removed)> wrote in message
> news:flWKd.230262$(E-Mail Removed)-kc.rr.com...
> >
> > My question is why dynamically allocate an array of Object pointers. Why

> not
> > just just
> >
> > Object *obparray[9];
> >
> > ??
> >
> > Is memory that sparse now days?
> >

>
> It's possible that Object doesn't have a default constructor.


Oops! my bad - I misunderstood your post.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swingworker thread or just a plain thread? smily Software 0 07-08-2010 04:03 PM
Entity framework or just plain old SqlDataReader? Andy B. ASP .Net 3 02-24-2009 06:56 PM
Just plain crap? mike3 C++ 6 06-23-2007 12:40 AM
is there a x64 TV Tuner that just plain works? Arthur Rosene Windows 64bit 3 12-18-2005 01:30 AM
Interesting Software Sites (?) or Just Plain SCAMS (?) Bay0Wulf Computer Support 3 12-01-2003 09:53 AM



Advertisments