Hi everyone, I write a project about a banking system . In homework professor told us that "Your code must not have any memory leaks. You will lose points if you have memory leaks in your program even though the outputs of the operations are correct." How can I check whether my code has memory leak or not?

Recommended Answers

All 7 Replies

Check variables that you allocated memory on, and make sure you deleted it somewhere else in the code. If you forget to free any memory, that's a memory leak.

How can I check whether my code has memory leak or not?

  • objects allocated memory using 'new' must be deleted.
  • anytime you access an array out of bounds
  • anytime you try to dereferrence a NULL pointer.
  • anytime you leave a dangling pointer, for example:
int* a, b, c;

a = new int;
b = a;
c = a;
delete a;

//What are b and c pointing to..??!?  NOTHING!

This is all I can think of for now as far as memory leakage.

post your code and you will get more help

Thanks for your help..:) If I post my code, someone who studies in my university can find the code and copy it. Because we like research on internet and copy it:D So it become plagiarism and I can go to discipline. I am frightened about this issue.:D

use valgrind --leak-check=full it will tell you where you have a leak ;)

Thanks for your help..:) If I post my code, someone who studies in my university can find the code and copy it. Because we like research on internet and copy it:D So it become plagiarism and I can go to discipline. I am frightened about this issue.:D

Ok then.

Here are some tips.

1) Whenever you have "new" keyword used, match it with a "delete"
keyword. Make sure there is a 1 to 1 relationship there.

2) Instead of raw pointers, use smart pointers or auto_ptr.
They will destroy the pointer for you.

3) Or PM someone and they might help you out a bit more, if you want

Thanks for your suggestions, I will try to apply these in my code.:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.