There's no way to know it from inside the program. There are memory debuggers capable of detecting and measuring memory leaks, and some system monitors report how much memory a program is using. You can use one to find when a program is using more memory than it should.
void main()
{
int *pi = newint(0x12345678);
int *pAry = newint[10];
delete pi;
return;
// Here, memory leak.Because we need 'delete []pAry'.
// How can I found it here automatically?
}
If you want to "find a memory leak" I suggest using a debugger. But you shouldn't need to.
Just delete the dynamic memory you used when you don't need it anymore... that's all a memory leak is.
"A memory leak in computer science is a particular type of unintentional memory consumption by a computer program where the program fails to release memory when no longer needed"
When you don't need a dynamically allocated variable anymore, delete it. Or use a garbage collection algorithm, but I don't know anything about that.
Microsoft's MFC has a debug_new that's mapped into debug builds.
Each allocation is recorded and tied up with frees. At the end of the program run, the program lists allocations, the first few bytes of the buffer, the file and line number where the allocation occurred. It's pretty cool.