Shared_ptr; t(new char[num]) 意味着内存泄漏?

发布于 2024-12-11 19:18:47 字数 188 浏览 0 评论 0原文

shared_ptr<void> t(new char[num])

意味着内存泄漏?

如果是这样,在这种情况下正确的做法是什么?

我应该使用shared_array<>反而?

我正在手动编辑“t”指向的字节,以便稍后在 TCP 流中传输。

shared_ptr<void> t(new char[num])

means memory leak?

If so, what is the correct practice in this case.

should I use shared_array<> instead?

I'm editing the bytes pointed by 't' manually for later transfer in a TCP Stream.

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

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

发布评论

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

评论(5

江湖正好 2024-12-18 19:18:47

意味着内存泄漏?

不,这意味着未定义的行为。 (这可能有任何症状,包括内存泄漏。)对 delete 的调用必须与对 new 的调用相匹配。你的没有。您可以使用new[]进行分配,但可以使用delete进行销毁。

如果是这样,在这种情况下正确的做法是什么?我应该使用shared_array<>吗?相反?

有两个简单的选择。您可以使用 shared_array

shared_array<char> t(new char[num])
t[7] = 42;

或者,您可以使用 shared_ptrstd::vector

shared_ptr<std::vector<char> > t(new std::vector<char>(num))
(*t)[7] = 42;


EDIT: Thanks to @Dennis Zickefoose for gently pointing out an error in my thinking. Parts of my answer are rewritten.

means memory leak?

No, it means undefined behavior. (Which could have any symptom, including memory leak.) The call to delete must match the call to new. Yours doesn't. You allocate with new[] but destroy with delete.

If so, what is the correct practice in this case. Should I use shared_array<> instead?

There are two easy choices. You can use shared_array:

shared_array<char> t(new char[num])
t[7] = 42;

Or, you could use a shared_ptr to a std::vector:

shared_ptr<std::vector<char> > t(new std::vector<char>(num))
(*t)[7] = 42;


EDIT: Thanks to @Dennis Zickefoose for gently pointing out an error in my thinking. Parts of my answer are rewritten.

你穿错了嫁妆 2024-12-18 19:18:47

正如我所见,您在 Q 中提到的 void 是一个拼写错误,因为在 void * 上调用删除是标准保证的未定义行为。

对于另一种数据类型,

您必须向 shared_ptr 提供自定义deletor,以便可以调用delete []

例如:

例如:

template<typename T>
struct Customdeleter
{
   void operator()(T* p)
   {
      delete [] p;
   }
};

并调用为:

shared_ptr<char> sp(new char[num], Customdeleter<char>());

编辑:
既然您澄清在注释中使用的是 Boost 而不是 TR1(据我所知 TR1 没有共享数组),

您可以使用 shared_array

shared_array<char> sp(new char[num])

As I see the void You mention in the Q is a typo, Since Calling delete on a void * is guaranteed Undefined Behavior by the Standard.

For another data type,

You will have to provide your custom deletor to the shared_ptr so you can call delete [].

Eg:

For example:

template<typename T>
struct Customdeleter
{
   void operator()(T* p)
   {
      delete [] p;
   }
};

And invoke as:

shared_ptr<char> sp(new char[num], Customdeleter<char>());

EDIT:
Since you clarified are using Boost in comments and not TR1(AFAIK TR1 doesn't have shared_array)

You can use shared_array:

shared_array<char> sp(new char[num])
独﹏钓一江月 2024-12-18 19:18:47

我想我明白你的来源 - 你需要 void * 指针,以便稍后可以将其转换为要序列化的最终类型。但正如其他人指出的那样,您无法删除 void* 指针,shared_ptr 的代码也不能删除。

由于您要分配一个 char 数组,因此这应该是您使用的智能指针的类型:

shared_array<char> t(new char[num]);

将原始 char 指针转换为另一种类型不应该比转换 void* 更成问题指针。

I think I see where you're coming from - you want void * pointers so you can later cast it to the final type you're serializing. But as others have pointed out, you can't delete a void* pointer, and neither can the code for shared_ptr.

Since you're allocating an array of char, that should be the type of smart pointer you use:

shared_array<char> t(new char[num]);

Casting the raw char pointer to another type shouldn't be any more of a problem than casting a void* pointer.

断爱 2024-12-18 19:18:47

您正在对 void* 调用 delete,这是未定义的行为。

我使用 void* 的原因是因为我分配了“num”个字节来存储不同类型的变量,例如前 4 个字节表示双精度,接下来的 2 个字节很短。

然后使用结构或联合。

You're calling delete on void* which is undefined behavior.

the reason I'm using a void* is because I'm allocating 'num' bytes for storage of different types of variables, like the first 4 bytes represent a double, next 2 bytes are short..

Use a struct or union then.

东风软 2024-12-18 19:18:47

我不知道C++11是否有shared_array, 但是Boost 可以——你应该使用它。

I don't know if C++11 has a shared_array, but Boost does — you should use that instead.

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