Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I recently started studying c++ and came across this class:
C++
class ShapeList {
        public:
          ShapeList() { }
          ~ShapeList() { }  
          void insert(const Shape& shape) {
                 shapes.insert(new Shape(shape));
            }
            void print () const {
            for (listType::const_iterator it = shapes.begin(); it != shapes.end(); ++it) {
            std::cout << *[*it] << std::endl;
            }
            }
            private:
            typedef std::set<Shape*> listType;
            listType shapes;
            };

I can't figure out how to fix this memory leak:
C++
shapes.insert(new Shape(shape));

-google only tells me how to delete one kind of "new" object:
C++
char* str1 = new char [30];
  delete [] str1;

Also, I can't figure out how to solve this questions:
- If you are calling a function like void f(ShapeList sl) some memory leaks will occur. Explain the causes and propose fixes.
Is this is because of the memory leak above? I can't see other problems and google is scarce on this topic..

- Modify the class ShapeList to support stream output, i.e. cout << sl , where sl is an instance fo class ShapeList.

Thank you for your time.
Posted
Updated 15-Sep-15 9:09am
v2
Comments
Patrice T 15-Sep-15 16:27pm    
May be it is time to jump to modern time and get managed !
C# or .net Java VB D ....
Member 11986577 15-Sep-15 16:38pm    
Well, I was told c++ is good to start with. :)
Patrice T 15-Sep-15 17:02pm    
C and C++ have among the most cryptic syntax.
[no name] 16-Sep-15 12:34pm    
Well I think in c# comparable happens, think about These hidden "stupid" iDisponable things. So c++ is not bad to learn proper coding in the meaning "what I allocate I'm responsible for it".

You can delete any object that has been instantiated by the new operator. The issue is more that you have to decide when you can delete the objects, at the very end of your program or at some earlier time, when you know you will no longer need them.

For your second question you probably need to create an override of the ToString method, which returns a stream of the objects in the list.
 
Share this answer
 
One way to avoid memory leak os to avoid using pointers.
C++
std::set<shape>

And make appropriate changes to the code.

Otherwise, using shared pointers or other kind of smart pointers can really help avoiding leaks particulary if an exception occurs or if the code is not linear.
 
Share this answer
 
v2
Quote:
I can't figure out how to fix this memory leak:
In ShapeList destructor, you have to traverse the list and delete every Shape instance.


Quote:
If you are calling a function like void f(ShapeList sl) some memory leaks will occur. Explain the causes and propose fixes.
Is this is because of the memory leak above?
Yes.


Quote:
- Modify the class ShapeList to support stream output, i.e. cout << sl , where sl is an instance fo class ShapeList.
You either
  • provide a method returning a string representation of the ShapeList, e.g.
    C++
    string ShapeList::to_string();
or
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900