BeeBand
BeeBand

Reputation: 11503

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

Upvotes: 1

Views: 133

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129464

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 - e.g. 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.

Upvotes: 5

Related Questions