1

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);
BeeBand
  • 10,953
  • 19
  • 61
  • 83

1 Answers1

5

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 2
    +1; yes I know the comments also contain an answer but I don't like the practice of answering 'simple' questions in comments on this site. – Bathsheba Aug 05 '13 at 11:15