简体   繁体   English

如果我将POD结构分配给另一个POD结构,是否存在内存泄漏?

[英]If I assign a POD struct to another POD struct, is there any memory leak?

For example: 例如:

struct Vertex
{
  int x;
  int y;
};

Vertex makeVertex(int xpos, int ypos)
{
  Vertex tmp = {xpos, ypos};
  return tmp;
}

Would I get a memory leak if I did this?: 如果这样做,是否会发生内存泄漏?:

Vertex a = makeVertex(30,40);
a = makeVertex(5, 102);

This is perfectly safe. 这是绝对安全的。

Memory leaks are caused by (mis)using pointers and memory allocations (typically calls to new that aren't followed by calls to delete , but more complex cases are often where the real problems occur - eg not completing the "rule of three (or five)" when dealing with classes that have calls to new ). 内存泄漏是由于(错误地)使用了指针和内存分配(通常是对new调用而不是对delete的调用,但是更复杂的情况通常是真正的问题发生的地方,例如,未完成“三个规则(或五)”,处理要调用new类。

And of course when using the C style calls to malloc and siblings the code should have a corresponding free call. 当然,当使用C样式调用malloc和同级对象时,代码应具有相应的free调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM