Posted: Sat Jan 29, 2005 9:39 am Post subject: Question on delete [] vs just plain delete
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!
Posted: Sat Jan 29, 2005 11:11 am Post subject: Re: Question on delete [] vs just plain delete
"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
Posted: Sat Jan 29, 2005 11:20 am Post subject: Re: Question on delete [] vs just plain delete
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!
Posted: Sat Jan 29, 2005 2:47 pm Post subject: Re: Question on delete [] vs just plain delete
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.
Posted: Sat Jan 29, 2005 7:58 pm Post subject: Re: Question on delete [] vs just plain delete
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!
Posted: Sat Jan 29, 2005 8:17 pm Post subject: Re: Question on delete [] vs just plain delete
"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).
Posted: Sun Jan 30, 2005 1:00 am Post subject: Re: Question on delete [] vs just plain delete
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!
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