2

so i would like to have vector<OtherClassName> theVector as a member of a BaseClass

i am wondering in many ways that i can get a memory leaks...

will doing this results in memory leaks?

BaseClass::someFunction(){
   OtherClassName * c = new OtherClassName();
   theVector.push_back((*c));
}

i'm a beginner in C++, learning from the internet.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
jujiyangasli
  • 360
  • 3
  • 13
  • If you want to learn C++ you might want to read a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Learning C++ in bits and pieces from the internet is a slow and error-prone process. – Blastfurnace Mar 27 '13 at 19:34

2 Answers2

1

will doing this result in memory leaks?

Yes, this will result in a memory leak. Every object allocated with new must be destroyed with delete. Failing to do so causes a memory leak.

In particular, what you are storing in your vector here is a copy of the object allocated with new. If you want your container to hold objects of a certain class, it is enough to do:

BaseClass::someFunction()
{
    OtherClassName c;
    theVector.push_back(c);
}

Notice that the vector, like all containers of the C++ Standard library, has value semantics: this means that what you are inserting in the vector is a copy of the object you pass to push_back(). Further modifications to the original objects won't be reflected by the state of the object contained in the vector, and vice versa.

If you want this to happen, i.e. if you need reference semantics, you will have to let your vector contain (possibly smart) pointers. For instance:

#include <memory>

// theVector would be declared as:
// std::vector<std::shared_ptr<OtherClassName>> theVector;

BaseClass::someFunction()
{
    std::shared_ptr<OtherClassName> pC = std::make_shared<OtherClassName>();
    theVector.push_back(pC);
}

Manual memory management through new and delete is considered bad programming practice in Modern C++, because it easily leads to memory leaks or undefined behavior, and negatively affects the design of your program in terms of robustness, readability, and ease of maintenance.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • thanks, this helps a lot.. the way you explains _value semantics_ and _reference semantics_ really help me.. :) – jujiyangasli Mar 27 '13 at 22:44
  • @sftrabbit: That's just thanks to an overly generous OP who decided to award me a bounty. Apart from this, it seems you're doing better than me lately ;) – Andy Prowl Mar 28 '13 at 00:13
0

Classes that dynamically create anything should have a destructor that will free the memory when the object is destroyed. If you don't have one you have memory leaks. Any memory taken by a new statement must have a corresponding delete statement or you will have a memory leak. As your class is written now it will have memory leaks since you never free the memory again. Your destructor should simply go through the vector and free the memory from every pointer it stores.

Edward Goodson
  • 302
  • 1
  • 11