Why does this program NOT cause a memory leak

Yes- I know the question is strange

Page 1 of 1

8 Replies - 1023 Views - Last Post: 09 November 2010 - 07:20 PM Rate Topic: -----

#1 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Why does this program NOT cause a memory leak

Posted 09 November 2010 - 03:34 PM

Hi- I'm playing around with dynamic allocation in c++ and I was trying to write a program that causes a memory leak. But for some reason the memory is getting cleared when I end the program. Am I doing something wrong?


int main(void)
{
	while(true)
	{
		int *p;
		p=new int;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why does this program NOT cause a memory leak

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 03:42 PM

It is a memory leak as long as the program is running, but when the program ends the memory is reclaimed by the OS.
Was This Post Helpful? 2
  • +
  • -

#3 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 03:45 PM

View PostJackOfAllTrades, on 09 November 2010 - 04:42 PM, said:

It is a memory leak as long as the program is running, but when the program ends the memory is reclaimed by the OS.


In class today my prof said that there was some kind of memory allocation that wouldn't be reclaimed by the OS and you had to reboot to fix. Is there another type of pointer or memory allocation that I forgot about? I'm not trying to write a virus or anything. I'm just curious. Thanks for the quick reply.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 03:48 PM

What OS is he talking about, and when did it hit the market?
http://en.wikipedia....mory_protection
Was This Post Helpful? 1
  • +
  • -

#5 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 03:55 PM

View PostCTphpnwb, on 09 November 2010 - 04:48 PM, said:

What OS is he talking about, and when did it hit the market?
http://en.wikipedia....mory_protection


Oh, thanks. I must have misunderstood what she said.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 03:58 PM

Or it could be another moron professor. Remember the old canard..."Those who can, do...those who can't teach"
Was This Post Helpful? 2
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 06:48 PM

There are situations where the OS can not recover all resources allocated by a process when the process exits. For example if the resources are under the control of another process or another piece of hardware. These kinds of situations do not arise very often but you will on occasion find that exiting an application does not restore sanity to the OS. Another (probably more common) situation occurs when there is an error in the OS's handling of the memory. It really is not uncommon for one to have trouble recovering memory without a reboot but these situations generally represent an error state that is not necessarily the easiest to create (at least on purpose).

What I find more often (in the case of FireFox) is that when trying to exit an application with a memory leak the process locks up in such a way that the OS can't seem to kill it. I have no idea what creates the situation as I generally just reboot and curse Windows.

People get these ideas that "Oh memory leaks and stuck file handles are a thing of the past" but they really are not. Always release your memory and always close your files, release handles, close connections, close sockets etc... because (at least in the case of Windows) the OS is full of bugs and chances are you will stumble upon one.

So what your professor said was not entirely false and you would do well to avoid ignoring proper resource handling procedures.
Was This Post Helpful? 2
  • +
  • -

#8 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 07:15 PM

In the case of the OP's code, it is more likely the system will crash (if left to run long enough). A memory equivalent of a fork() bomb.
Was This Post Helpful? 0
  • +
  • -

#9 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: Why does this program NOT cause a memory leak

Posted 09 November 2010 - 07:20 PM

View PostKYA, on 09 November 2010 - 08:15 PM, said:

In the case of the OP's code, it is more likely the system will crash (if left to run long enough). A memory equivalent of a fork() bomb.


Actually, I let it run until it filled up 8gb of ram and my 13gb swapspace. On Ubuntu Linux when all of the memory and swapspace was full the OS terminated the program and freed all of the memory.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1