删除指针:为什么这个工作!! ?? [英] delete pointers: why is this working!!??
问题描述
我相信删除已经删除的指针应该给我一个
分段错误!那么,为什么以下工作呢?我正在使用gcc
版本3.0.2。
#include< stdio.h>
#include< string>
使用命名空间std;
int main()
{
string * j = new string(" this prog应该转储核心!);
printf(" \ n%s \ n",j-> c_str());
删除j;
删除j;
}
i believe deleting an already delete pointer should give me a
segmentation fault! Why, then, is the following working? I am using gcc
version 3.0.2.
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}
推荐答案
* mufasa:
我相信删除已经删除的指针应该给我一个
分段错误!那么,为什么以下工作呢?我正在使用gcc
版本3.0.2。
#include< stdio.h>
#include< string>
使用命名空间std;
int main()
{
string * j = new string(" this prog should dump core!");
printf(" \ n%s \\ \\ n",j-> c_str());
删除j;
删除j;
}
i believe deleting an already delete pointer should give me a
segmentation fault! Why, then, is the following working? I am using gcc
version 3.0.2.
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}
它'未定义行为,这意味着就标准而言,任何结果都是可接受的。
一般建议,因为你正在学习语言:
1)不要使用原始指针。
2)不要使用原始指针。
3)不要......你明白了。
另外,考虑使用C ++ iostream,它比C printf更安全类型。
>
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的事情是什么?
It''s Undefined Behavior, which means that any result whatsoever is
acceptable as far as the standard is concerned.
General advice as you''re learning the language:
1) Don''t use raw pointers.
2) Don''t use raw pointers.
3) Don''t ... You get the idea.
Also, consider using C++ iostreams, which are more type-safe than C printf.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
mufasa写道:
mufasa wrote:
我相信删除一个已经删除指针应该给我一个分段错误!
不,它应该给你不明确的行为。
那么,为什么以下工作呢?
因为这是未定义行为的一个实例。
我使用的是gcc 3.0.2版。
#包括< stdio.h>
#include< string>
使用命名空间std;
int main()
{
string * j = new string(" this prog应该转储核心!);
printf(" \ n%s \ n",j-> c_str());
删除j;
删除j;
}
i believe deleting an already delete pointer should give me a
segmentation fault!
No, it should give you undefined behavior.
Why, then, is the following working?
Because that is one instance of "undefined behavior".
I am using gcc version 3.0.2.
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}
std :: string类可能已经使用了引用计数分配
实际上是字符串内容。如果引用计数降至零,则引用计数规则可以是删除
内容;如果引用计数为
为负数,则不要删除任何内容。
你应该参考实际的实现来查看发生了什么
那里。
不要做双重删除,这就是全部。
" mufasa" ; <所以***** @ gmail.com>在消息中写道
news:11 ********************** @ l41g2000cwc.googlegr oups.com ...
the std::string class might''ve used a reference counting allocation for the
actually string content. The reference counting rule can be "delete the
content if reference count drops to zero; if the reference count is
negative, don''t do any deletion".
you should refer to the actually implementation to see what is going on
there.
don''t do double deletion, that''s all.
"mufasa" <so*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
我相信删除已经删除的指针应该给我一个
分段错误!那么,为什么以下工作呢?我正在使用gcc
版本3.0.2。
#include< stdio.h>
#include< string>
使用命名空间std;
int main()
{
string * j = new string(" this prog should dump core!");
printf(" \ n%s \\ \\ n",j-> c_str());
删除j;
删除j;
}
i believe deleting an already delete pointer should give me a
segmentation fault! Why, then, is the following working? I am using gcc
version 3.0.2.
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}
这篇关于删除指针:为什么这个工作!! ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!