简体   繁体   中英

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 ).

And of course when using the C style calls to malloc and siblings the code should have a corresponding free call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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