关于删除[]与仅删除的问题 [英] Question on delete [] vs just plain delete
问题描述
我是c ++的新手。我最近花了大量的时间来解决一个看似无害的代码片段。虽然我把这段代码缩小了作为罪魁祸首,但我不明白为什么。可以
一些大师有助于启发我吗?谢谢。
//我创建了一个指向对象指针的指针数组:
Object ** obs = new Object * [9] ;
//之后我填充这个数组:
for(int i = 0; i< 9; i ++){
obs [i] = new Object(i);
}
//使用它们后我删除了数组:
for(int i = 0; i< 9; i ++){
删除obs [i];
}
删除[] obs;
删除obs; //这就是问题,在执行程序期间
被绞死。
显然,上面引起了未定义的行为。但我不知道为什么
我不应该这样做。再次感谢您的分享!
干杯,
Damon
" DamonChong" <所以******** @ excite.com>在消息中写道
新闻:11 ********************* @ c13g2000cwb.googlegro ups.com ...
我是c ++的新手。我最近花了大量时间来解决一条看似无害的代码。虽然我把这段代码缩小为罪魁祸首,但我不明白为什么。可以帮助一些大师帮我启发吗?谢谢。
虽然大师回答了更难的问题,但我会把这个问题写下来。
//我创建了一个指向对象指针的指针数组:
///////////太好了,你已经分配了一系列指向对象的指示
//之后我填充了这个数组:
for(int i = 0; i< 9; i ++){
obs [i] = new Object(i);
///////////现在你已经分配了这些物品。 //
//使用它们后我删除了数组:
for(int i = 0; i< 9; i ++){
删除obs [i] ;
///////////现在你已经解除了对象的分配。
}
删除[] obs;
///////////现在你已经取消了数组。
///////// //好所以你已经解除了你分配的所有东西。什么都没有
留下来解除分配。
///////////然而......
删除obs; //这就是问题,在执行过程中程序挂了。
///////////所以上一行挂起了你要解除分配的东西
你不应该
你好Damon
Efrat已经回答了你的帖子,但我建议你不要使用
new / delete作为新手C ++程序员。而是专注于标准库
并且不要使用指针。
指针很难,最好不要使用直到你理解基础知识
$ b $ C ++的b - 包括标准库。通过这条路线,您还可以了解现金C ++中实际指针的使用情况。
/ Peter
DamonChong <所以******** @ excite.com> skrev i en meddelelse
news:11 ********************* @ c13g2000cwb.googlegro ups.com ...
我是c ++的新手。我最近花了大量时间来解决一条看似无害的代码。虽然我把这段代码缩小为罪魁祸首,但我不明白为什么。可以帮助一些大师帮我启发吗?谢谢。
//我创建了一个指向对象指针的指针数组:
对象** obs = new Object * [9];
> //之后我填充这个数组:
for(int i = 0; i< 9; i ++){
obs [i] = new Object(i);
}
//使用它们后我删除了数组:
for(int i = 0; i< 9; i ++){
删除obs [i ];
}
删除[] obs;
删除obs; //这就是问题,在执行过程中程序挂了。
显然,上面引起了不确定的行为。但我不知道为什么我不应该这样做。再次感谢您的分享!
干杯,
Damon
2005年1月29日01:39:32 - 0800,DamonChong < so ******** @ excite.com>
写道:
delete [] obs;
删除obs; //这是问题,在执行程序期间
通常你在你分配的任何数组上使用delete []告诉
编译器那个ptr你正在删除是一个数组。
它只是你需要学习的那些不明显的c ++规则之一:)
无论如何我建议你使用vector<>而不是数组因为它
更安全,更方便。查看在线帮助/书籍以获取信息。
STL一般可以帮到你很多。
/ ajk
- -
这些是我的原则。如果你不喜欢他们,我还有其他人。
Groucho Marx。
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
"DamonChong" <so********@excite.com> wrote in message
news:11*********************@c13g2000cwb.googlegro 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
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********@excite.com> skrev i en meddelelse
news:11*********************@c13g2000cwb.googlegro 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
On 29 Jan 2005 01:39:32 -0800, "DamonChong" <so********@excite.com>
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.
这篇关于关于删除[]与仅删除的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!