关于删除的问题 [英] Question about delete
问题描述
嗨:
我有一本书中的问题--- C ++入门,第677页。
char * arena = new char [sizeof Image];
图片* ptr = new(竞技场)图片(Quasimodo);
经过以上两行,竞技场和ptr指向同一个内存。
如果我删除竞技场,那个内存将被释放,对吗?
现在ptr是一个悬空指针吗?
谢谢。
john
John在新闻中写道:c3 **************************@posting.google.c om
comp.lang.c ++:
嗨:
我有一本书中的问题--- C ++入门,第677页。
char * arena = new char [sizeof Image];
Image * ptr = new(arena)Image(Quasimodo);
在上面两行之后,arena和ptr指向同一个内存。 />如果我删除竞技场,
删除[]竞技场;
那个内存会被释放吧?
现在ptr是一个悬空指针吗?
是的,竞技场也是如此。
Rob。
-
http://www.victim-prime.dsl.pipex.com/
John写道:嗨:
我有一本书中的问题--- C ++入门,第677页。
char * arena = new char [sizeof Image];
图像* ptr = new(竞技场)图像(Quasimodo) );
在上面两行之后,竞技场和ptr指向同一个内存。
如果我删除竞技场,该内存将被释放,对吧?
是ptr是一个悬空指针吗?
如果你说删除区域,你有未定义的行为。需要
才能删除[]竞技场。
是的,ptr在此之后晃来晃去。另外,你可能也会失去
在Image类中分配的任何resorces,因为它永远不会被破坏。
想象一下:
class Image {
std :: string name;
char pixels [100000];
};
上面的代码片段会泄漏std :: string在内部分配
的资源。
Ron Natalie写道:< blockquote class =post_quotes> John写道:
嗨:
我在书中提出了一个问题--- C ++入门,第677页。 />
char * arena = new char [sizeof Image];
Image * ptr = new(arena)Image(Quasimodo);
以上两行之后,竞技场和ptr指向相同的记忆。
如果我删除竞技场,那个记忆就会被释放,对吗?
现在ptr是一个悬垂的指针吗?
如果你说删除区域,你有未定义的行为。它需要
才能删除[]竞技场。
是的,ptr在此之后晃来晃去。另外,你也可能会丢失在Image类中分配的任何resorces,因为它永远不会被破坏。
想象一下:
class Image {
std ::字符串名称;
字符像素[100000];
public:
Image(const std :: string& n ):name(n){}
};
上面的代码片段会泄漏std :: string在内部分配的资源。
....并且要打你需要明确地调用Image的d-tor:
ptr->〜图像();
在尝试释放内存之前。
只是我的
Hi:
I have a question from the book---C++ primer, page 677.
char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );
After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?
Thanks.
john
John wrote in news:c3**************************@posting.google.c om in
comp.lang.c++:
Hi:
I have a question from the book---C++ primer, page 677.
char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );
After the above two lines, arena and ptr point to the same memory.
If I "delete arena",
delete [] arena;
that memory will be released, right?
Is ptr an dangling pointer now?
Yes and so is arena.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
John wrote:Hi:
I have a question from the book---C++ primer, page 677.
char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );
After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?
If you say delete area, you have undefined behavior. It needs
to be delete[] arena.
Yes ptr is dangling after that. In addition, you may also lose
any resorces allocated in the Image class since it never gets
destructed.
Imagine:
class Image {
std::string name;
char pixels[100000];
};
Your above code snippet will leak resources that std::string allocates
internally.
Ron Natalie wrote:John wrote:Hi:
I have a question from the book---C++ primer, page 677.
char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );
After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?
If you say delete area, you have undefined behavior. It needs
to be delete[] arena.
Yes ptr is dangling after that. In addition, you may also lose
any resorces allocated in the Image class since it never gets
destructed.
Imagine:
class Image {
std::string name;
char pixels[100000];
public:
Image(const std::string& n) : name(n) {}
};
Your above code snippet will leak resources that std::string allocates
internally.
....and to fight that you need to explicitly call the Image''s d-tor:
ptr->~Image();
before attempting to free the memory.
Just my
这篇关于关于删除的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!