在 C++ 中的结构上使用 memset

发布于 2024-08-31 13:22:59 字数 528 浏览 8 评论 0原文

我正在努力为我的工作修复旧代码。目前它是用 C++ 编写的。他们将静态分配转换为动态分配,但没有编辑 memsets/memcmp/memcpy。这是我的第一次编程实习,所以我的问题就像新手一样。

以下代码是用 C 编写的,但我想用 C++ 编写(我读到 malloc 在 C++ 中不是一个好的做法)。我有两种情况:首先,我们创建了 f 。然后使用 &f 来填充零。第二个是指针*pf。我不确定如何将 pf 设置为全 0,就像前面 C++ 中的示例一样。

您可以直接执行 pf = new foo 而不是 malloc,然后调用 memset(pf, 0, sizeof(foo)) 吗?

struct foo { ... } f;
memset( &f, 0, sizeof(f) );

//or

struct foo { ... } *pf;
pf = (struct foo*) malloc( sizeof(*pf) );
memset( pf, 0, sizeof(*pf) );

I am working on fixing older code for my job. It is currently written in C++. They converted static allocation to dynamic but didn't edit the memsets/memcmp/memcpy. This is my first programming internship so bare with my newbe-like question.

The following code is in C, but I want to have it in C++ ( I read that malloc isn't good practice in C++). I have two scenarios: First, we have f created. Then you use &f in order to fill with zero. The second is a pointer *pf. I'm not sure how to set pf to all 0's like the previous example in C++.

Could you just do pf = new foo instead of malloc and then call memset(pf, 0, sizeof(foo))?

struct foo { ... } f;
memset( &f, 0, sizeof(f) );

//or

struct foo { ... } *pf;
pf = (struct foo*) malloc( sizeof(*pf) );
memset( pf, 0, sizeof(*pf) );

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

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

发布评论

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

评论(4

奶气 2024-09-07 13:22:59

是的,但前提是 foo 是 POD。如果它有虚拟函数或任何其他远程 C++ 风格的东西,请不要在其上使用 memset,因为它会破坏结构/类的所有内部结构。

您可能想要做的而不是 memset 是为 foo 提供一个构造函数来显式初始化其成员。

如果要使用new,不要忘记相应的delete。更好的是使用shared_ptr:)

Yes, but only if foo is a POD. If it's got virtual functions or anything else remotely C++ish, don't use memset on it since it'll stomp all over the internals of the struct/class.

What you probably want to do instead of memset is give foo a constructor to explicitly initialise its members.

If you want to use new, don't forget the corresponding delete. Even better would be to use shared_ptr :)

旧街凉风 2024-09-07 13:22:59

你可以吗?是的,可能是。你应该吗?不。

虽然它可能会起作用,但您将失去构造函数为您构建的状态。除此之外,当您决定实现该结构的子类时会发生什么?然后您就失去了 C++ OOP 提供的可重用代码的优势。

相反,您应该做的是创建一个构造函数来为您初始化成员。这样,当您稍后对该结构进行子类化时,您只需使用此构造函数来帮助您构造子类。这是免费、安全的代码!使用它!

编辑:需要注意的是,如果您已经有一个巨大的代码库,请不要更改它,直到您开始对结构进行子类化。它就像现在一样工作。

Can you? Yes, probably. Should you? No.

While it will probably work, you're losing the state that the constructor has built for you. Adding to this, what happens when you decide to implement a subclass of this struct? Then you lose the advantage of reuseable code that C++ OOP offers.

What you ought to do instead is create a constructor that initializes the members for you. This way, when you sublass this struct later on down the line, you just use this constructor to aid you in constructing the subclasses. This is free, safe code! use it!

Edit: The caveat to this is that if you have a huge code base already, don't change it until you start subclassing the structs. It works as it is now.

南…巷孤猫 2024-09-07 13:22:59

是的,那会起作用。但是,我不认为 malloc 一定是不好的做法,而且我不会为了改变它而改变它。当然,您应该确保始终正确匹配分配机制(new->delete、malloc->free 等)。

您还可以向结构添加构造函数并使用它来初始化字段。

Yes, that would work. However, I don't think malloc is necessarily bad practice, and I wouldn't change it just to change it. Of course, you should make sure you always match the allocation mechanisms properly (new->delete, malloc->free, etc.).

You could also add a constructor to the struct and use that to initialize the fields.

不可一世的女人 2024-09-07 13:22:59

您可以new foo(这是C++中的标准方式)并实现一个初始化foo的构造函数,而不是使用memset

例如,

struct Something
{
    Something()
        : m_nInt( 5 )
    {

    }

    int m_nInt;
};

也不要忘记,当您完成对象时,是否使用new调用delete,否则最终会导致内存泄漏。

You could new foo (as is the standard way in C++) and implement a constructor which initialises foo rather than using memset.

E.g.

struct Something
{
    Something()
        : m_nInt( 5 )
    {

    }

    int m_nInt;
};

Also don't forget if you use new to call delete when you are finished with the object otherwise you will end up with memory leaks.

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