C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question on delete [] vs just plain delete

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
DamonChong
Guest





PostPosted: Sat Jan 29, 2005 9:39 am    Post subject: Question on delete [] vs just plain delete Reply with quote



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

Back to top
Efrat Regev
Guest





PostPosted: Sat Jan 29, 2005 11:11 am    Post subject: Re: Question on delete [] vs just plain delete Reply with quote



"DamonChong" <so_excited (AT) excite (DOT) com> wrote

Quote:
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.

Quote:

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

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

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

Quote:

// 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.
Quote:
}

// After using them i deleted the array:

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

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

Quote:
}

delete [] obs;

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


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

/////////// and yet....
Quote:

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




Back to top
Peter Koch Larsen
Guest





PostPosted: Sat Jan 29, 2005 11:20 am    Post subject: Re: Question on delete [] vs just plain delete Reply with quote



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" <so_excited (AT) excite (DOT) com> skrev i en meddelelse
news:1106991572.867157.21520 (AT) c13g2000cwb (DOT) googlegroups.com...
Quote:
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




Back to top
ajk
Guest





PostPosted: Sat Jan 29, 2005 2:47 pm    Post subject: Re: Question on delete [] vs just plain delete Reply with quote

On 29 Jan 2005 01:39:32 -0800, "DamonChong" <so_excited (AT) excite (DOT) com>
wrote:

Quote:
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.

Back to top
DamonChong
Guest





PostPosted: Sat Jan 29, 2005 5:42 pm    Post subject: Re: Question on delete [] vs just plain delete Reply with quote

Thanks alot. Kind of silly I suppose to make such mistake. ;P

Back to top
Jon
Guest





PostPosted: Sat Jan 29, 2005 7:58 pm    Post subject: Re: Question on delete [] vs just plain delete Reply with quote

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" <pkspaml (AT) mailme (DOT) dk> wrote

Quote:
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" <so_excited (AT) excite (DOT) com> skrev i en meddelelse
news:1106991572.867157.21520 (AT) c13g2000cwb (DOT) googlegroups.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






Back to top
Efrat Regev
Guest





PostPosted: Sat Jan 29, 2005 8:17 pm    Post subject: Re: Question on delete [] vs just plain delete Reply with quote

"DamonChong" <so_excited (AT) excite (DOT) com> wrote

Quote:
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 #include <boost/shared_ptr.hpp>

std::vector<boost::shared_ptr 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).



Back to top
Bradley
Guest





PostPosted: Sun Jan 30, 2005 1:00 am    Post subject: Re: Question on delete [] vs just plain delete Reply with quote


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" <so_excited (AT) excite (DOT) com> wrote

Quote:
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




Back to top
Efrat Regev
Guest





PostPosted: Sun Jan 30, 2005 9:23 am    Post subject: Re: Question on delete [] vs just plain delete Reply with quote

"Bradley" <bnorth1 (AT) kc (DOT) rr.com> wrote

Quote:

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.



Back to top
Efrat Regev
Guest





PostPosted: Sun Jan 30, 2005 9:24 am    Post subject: Re: Question on delete [] vs just plain delete Reply with quote


"Efrat Regev" <efrat_regev (AT) yahoo (DOT) com> wrote

Quote:
"Bradley" <bnorth1 (AT) kc (DOT) rr.com> wrote in message
news:flWKd.230262$ye4.191747 (AT) twister (DOT) rdc-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.



Back to top
Old Wolf
Guest





PostPosted: Sun Jan 30, 2005 8:06 pm    Post subject: Re: Question on delete [] vs just plain delete Reply with quote

Efrat Regev wrote:
Quote:
Specifically, you could write your code this way:

#include <vector

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

Or if Object doesn't have a default constructor,

#include #include
std::vector 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).

You don't need to go to those lengths; this would do,
if Object is copyable:

.. std::vector .. for(int i = 0; i != 9; ++i)
.. obs.push_back( Object(i) );


Back to top
msalters
Guest





PostPosted: Mon Jan 31, 2005 1:27 pm    Post subject: Re: Question on delete [] vs just plain delete Reply with quote


Efrat Regev wrote:
Quote:
Specifically, you could write your code this way:

#include <vector

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

Actually, you mean
std::vector
which is one vector with initially 9 elements, and not
9 vectors with initially no elements.

With your [9] declaration, the compiler will complain that it
can't assign Object(0) to an empty vector of Objects.

You could also be a bit clearer, and write the size only once:

#include <vector>

std::vector<Object> obs; // empty

for(int i = 0; i < 9; ++i)
obs.pushback( Object(i) ); // add one element at a time.

Now, there's only one occurence of 9 - if the vector should ever
change size, you can do so in one place.

HTH,
Michiel Salters


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.