Here is one very simple question(or at least at first sight).Let’s say we have the following procedure:
void procedure(void)
{
int x = new int;
x=42;
}
When I call to this procedure in my program it should lead to the so called memory leak, right?I’ve been encountering such bugs while working on a small project and then compiler was outputting something like : detected memory leak.Dumping memory block …Now I try to simulate a memory leak and see what compiler outputs but it doesn’t complain at all(I use Visual Studio).
Is there really a memory leak that’s my question .
Yes it is memory leak. You use C++ and calling new operator will reserve 4 bytes of memory on the heap. – delete is not called.
If you will cal this function 2 million times, about 8 MB of memory will leak.
In this short program part – it allocates memory and pointer is converted to int (hidden conversion). Some compilers may not detect tis situation, another may.