Memory leak somewhere?

stahta01

Joined Jun 9, 2011
133
The leak is likely in the code you did not post; but, I am a C programmer.
So, I might have missed a mistake in the posted code.

NOTE: If you posted the code in Code tags you are more likely to get a response.
Use the "Go Advanced" button to see the "#" code tag button.

Tim S.
 

stahta01

Joined Jun 9, 2011
133
If it is NOT worth you time to rewrite the code or you are cheating by posting this question then it not worth my time to help.

Tim S.
 

samin

Joined Oct 14, 2011
32
Me neither, I can't see any mistake in the posted code .. I think Also and as mentioned in the posted PDF file it would be necessary to rewrite the code as it must be will commented and easily understandable.
 

spinnaker

Joined Oct 29, 2009
7,837
Me neither, I can't see any mistake in the posted code .. I think Also and as mentioned in the posted PDF file it would be necessary to rewrite the code as it must be will commented and easily understandable.
Good point. I think what we really need to see is the code that is being compiled not the code in the PDF.


So where is the OP?
 

Thread Starter

Peytonator

Joined Jun 30, 2008
105
Yup, there's no need to compile it. The problem which I've found is that actually the destructor is not declared virtual, so both the base and derived classes call the base class destructor. Thus the _dept string still remains intact.
 

spinnaker

Joined Oct 29, 2009
7,837
Humm that one is a new one on me. I have be away from C++ for a while but I never remember having a need to declare a destructor virtual.

The issue is that you are saving an Manager object in an Employee point so when you delete it the Employee destructor gets called no the manager as it should be,

Perhaps declaring the desctructor variable fixes this but you need to be certain the manager destructor is being called or you will still have a memory leak.
 

MCrowe

Joined May 29, 2011
69
I dont understand why every question someone asks on here, the first thing someone does is jump on them telling them there lazy, or cheating and they're not answering there question...

Sure some coments are helpful, like telling a new person they are meant to use code tags, but after that, if you dont want to answer the question, then don't. It seems like the original question was a question asked in a previous exam, probably on paper not on a computer, so it would be resonable to expect the answer could be obtained without compiling the code, as it was...
 

Georacer

Joined Nov 25, 2009
5,181
When you do programming training, the least you can do when you work a problem is to type it, compile it and then check for compilation errors. Anything less isn't really considered effort.

Past paper or not, any question worth answering should at least be backed up with some effort from the OP.
 

ToBeFrank

Joined Nov 18, 2010
13
When you do programming training, the least you can do when you work a problem is to type it, compile it and then check for compilation errors. Anything less isn't really considered effort.
The problem in this case is compiling and running this example does not gain anything. It will appear to work just fine. One could argue you could then put it through valgrind or what not, but I wouldn't expect a student to know about tools like that. I have to agree with the OP that putting it in a compilable form is not necessary. In my opinion, for this particular example, anyone that can't see the problem and requires it to be compiled is probably not qualified to answer the question.
 

stahta01

Joined Jun 9, 2011
133
There are too many people posting question like this using smart phones during tests. That is why I asked them to post the text of the question. This would permit a Google search to confirm the question was posted by the school.

Note: I am a C programer learning C++ and one of my two guesses was the destructor was missing/wrong; So, I was almost right.

Edit: I have taken classes that reuse test questions from passed years.

Tim S.
 
Last edited:

ToBeFrank

Joined Nov 18, 2010
13
Sorry, didn't mean to single anyone out. It's really a C++ question so I wouldn't expect someone who doesn't use C++ regularly to recognize the problem right away.
 
Top