我可以删除使用 memcpy() 复制的类指针吗?

发布于 2024-11-04 02:03:01 字数 409 浏览 1 评论 0原文

以下代码片段有效吗?

class Foo
{
public:
    int a;
    SomeClass* pb;

    Foo() {...}
    Foo(const Foo& rhs)
    {
         this->a = rhs.a;
         memcpy(this->pb, rhs.pb, sizeof(SomeClass));
    }
    ~Request()
    {
        delete this->pb; // Suppose pb is a result of copy constructor,
                         // Is this valid or is there a better way?
    }
};

Is the following snippet of code valid?

class Foo
{
public:
    int a;
    SomeClass* pb;

    Foo() {...}
    Foo(const Foo& rhs)
    {
         this->a = rhs.a;
         memcpy(this->pb, rhs.pb, sizeof(SomeClass));
    }
    ~Request()
    {
        delete this->pb; // Suppose pb is a result of copy constructor,
                         // Is this valid or is there a better way?
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

_畞蕅 2024-11-11 02:03:01

您实际上尚未为 pb 分配任何内容。如果您使用 new 分配它,那么在析构函数中使用 delete 是完全有效的。

但是为什么哦为什么你要做一个memcpy?您应该使用类的复制构造函数并让它以适当的方式复制自身。 唯一 memcpy 远程可接受的情况是该类是 POD 类型的简单结构时。

You haven't actually allocated anything for pb. If you allocate it with new, then using delete in the destructor is perfectly valid.

But why oh why are you doing a memcpy? You should be using the copy constructor of the class and letting it copy itself in the appropriate fashion. The only time a memcpy would be remotely acceptable is when the class is a simple struct of POD types.

提赋 2024-11-11 02:03:01

您无法使用 memcpy 复制类。而memcpy只是复制内存,而不是分配任何东西。

You can't copy classes using memcpy. And memcpy is just copying memory, not allocating anything.

静若繁花 2024-11-11 02:03:01

它是有效的,因为它会编译。但它却充满了错误。您从未分配过任何内存,因此您需要首先执行此操作。但绝对没有理由使用 memcpy,而且在大多数情况下,它会做错误的事情。只需这样做:

Foo(const Foo& rhs)
    :a(rhs.a),
     pb(new SomeClass(*rhs.pb))
{    
}

或者更好的是,使用智能指针,您不必担心删除。或者更好的是,如果您保留该对象的唯一所有权,则根本不要使用指针。

It's valid, as in it will compile. But it is stricken with errors. You never allocated any memory, so you'd need to do that first. But there is absolutely no reason to use memcpy, and in most cases, it does the wrong thing. Just do this:

Foo(const Foo& rhs)
    :a(rhs.a),
     pb(new SomeClass(*rhs.pb))
{    
}

Or better yet, use a smart pointer, and you don't have to worry about deleting. Or better yet, don't use a pointer at all if you retain sole ownership of the object.

罪#恶を代价 2024-11-11 02:03:01

此代码片段是错误的,因为如果不调用 new 来分配内存,则无法调用 delete

此外,在大多数情况下,您的 memcpy 调用可能会导致分段错误,因为您正在复制到未初始化的指针。在调用 memcpy 之前,您应该为 SomeClass 指针分配内存。

This code snippet is wrong because you can't call delete without calling to new for allocation of memory.

Also your memcpy call can cause segmentation fault in most cases, because you are copying to unintialized pointer. You should allocate memort for SomeClass pointer before calling to memcpy.

天冷不及心凉 2024-11-11 02:03:01

将 memcpy 与 POD 结构/对象(“普通旧数据”——没有非默认构造函数或析构函数以及虚拟方法的东西)以外的任何东西一起使用都是灾难的根源。如果您修复程序,以便使用 memcpy 复制包含指向类项的“普通”指针的 POD 结构,则作为该结构的一部分复制的任何指针都必须与通过其他方式复制的指针进行相同的处理。必须“删除”任何此类指针的一份副本(或原始副本)——不多也不少。如果您复制指针并放弃原始指针,而没有对其调用“delete”,则应该对一个副本调用“delete”。

一般来说,应该避免在 C++ 中使用 memcpy;在 C 中,结构从来没有任何“隐藏”信息,这些信息可能会被 memcpy 留在不一致的状态,但在 C++ 中它们经常这样做(POD 结构除外)。尽管 C++ 被设计为与 C 大部分兼容,但某些东西在 C 中合法并不意味着它在 C++ 中是正确的。

Using memcpy with anything other than POD structures/objects ("plain old data"--things with no non-default constructors or destructors, and with virtual methods) is a recipe for disaster. If you fix your program so that you're using memcpy to copy a POD structure which contains an "ordinary" pointer to a class item, any pointers copied as part of that structure must be handled the same as pointers copied via other means. Exactly one copy (or the original) of any such pointer must be "deleted"--no more and no less. If you copy the pointer and abandon the original without calling "delete" on it, you should then call "delete" on exactly one copy.

In general, one should avoid using memcpy in C++; in C, structures never had any "hidden" information which could be left in an inconsistent state by memcpy, but in C++ they often do (with the exception of POD structures). Although C++ is designed to be mostly compatible with C, the fact that something is legal in C does not mean that it is proper in C++.

深居我梦 2024-11-11 02:03:01

您尚未使用 new 为该指针分配内存,因此绝对不应该对其使用 deletememcpy 没有为您分配任何内存。

具体来说,您应该调用 delete 的唯一指针是先前由 new 返回的指针或空指针。根据 C++03 标准 3.7.3.2/3-4,对任何其他指针使用 delete 都是未定义的行为。

第一个参数的值
提供给释放之一
标准中提供的功能
库可能是空指针值;
如果是这样,则调用释放
功能没有任何影响。 否则,
提供给操作员的值
标准库中的delete(void*)
应是返回的值之一
之前的调用
运算符 new(size_t)
或运算符
new(size_t, const std::nothrot_t&)

...[剪断]...

使用无效指针的影响
值(包括将其传递给
解除分配函数)是
未定义。)

You haven't allocated memory for that pointer using new, so you absolutely shouldn't be using delete on it. memcpy isn't allocating any memory for you.

Specifically, the only pointers you should be invoking delete on are pointers that have previously been returned by new, or a null pointer. To use delete on any other pointer is undefined behaviour, according to the C++03 standard, 3.7.3.2/3-4.

The value of the first argument
supplied to one of the deallocation
functions provided in the standard
library may be a null pointer value;
if so, the call to the deallocation
function has no effect. Otherwise, the
value supplied to operator
delete(void*) in the standard library
shall be one of the values returned by
a previous invocation of either
operator new(size_t)
or operator
new(size_t, const std::nothrow_t&)

...[snip]...

The effect of using an invalid pointer
value (including passing it to a
deallocation function) is
undefined.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文