Is this a mem leak?

Started by
27 comments, last by AndreTheGiant 20 years, 11 months ago

int main() {
 
 int eger = new int;

}
 
Is this a memory leak? I know it probably would have been just plain good practice to simply do this instead:

int main() {
 
 int eger = new int;
 delete eger;

}
 
But suppose you don''t, doesnt the OS get the memory back when your program terminates anyway? In other words, even though this program technically has a memory leak, couldnt you run this infinetly many times without problem because the OS will get the memory back at the end of each run? Or does it depend on which OS you are using? If it depends on the OS, then how does Windows behave? How does Linux behave? Thanks.
Advertisement
Normally the OS should release all the memory an application uses, after it''s done.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
In most cases the OS will clean your memory, but that''s VERY bad coding practice.
It is a leak.

It would be a problem if your program did this over and over again though. Just because the OS cleans up the process memory on closing doesn''t mean its okay to leak memory.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:Original post by AndreTheGiant
int main() {  int eger = new int;}  


Is this a memory leak? I know it probably would have been just plain good practice to simply do this instead:

int main() {  int eger = new int; delete eger;}  


But suppose you don''t, doesnt the OS get the memory back when your program terminates anyway? In other words, even though this program technically has a memory leak, couldnt you run this infinetly many times without problem because the OS will get the memory back at the end of each run?

Or does it depend on which OS you are using? If it depends on the OS, then how does Windows behave? How does Linux behave?

Thanks.


Err.... int* eger = new int?

.lick
That is a leak. Even if it is freed after the program exits, it won''t be freed for you to use later on in your program. I''d also note that, while Win2000 and XP and linux are pretty robust and will most likely free up any memory when your program exits, win 95/98 sucks for stuff like that. Run windows explorer 20 times in a row under win98 and you''ll run out of free memory and have to reboot. In short, don''t depend on the operating system to do your dirty work.
Shouldn''t it be a pointer?

-~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-
Someone once referred to this type of leak:

void main() {
int* eger = new int;
};

as a "memory dribble". Since it''s so small and the program ends almost immediately.

Regardless of whether the OS frees this memory at the end or not, the issue is getting into the habit of NEVER DOING THIS. Always delete the memory you allocate once you''re done with it.

Regards,
Jeff
It is absoultely NOT a leak, you still have a pointer to the allocated memory, it is a (very small) memory pool.

int* p = new int;
p = 0;

is a leak.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:
It is absoultely NOT a leak, you still have a pointer to the allocated memory, it is a (very small) memory pool.


So you''re saying that even after the function has finished, and the activation frame is removed from the stack, it''s not a leak because the address is still somewhere on the stack? Come on.

This topic is closed to new replies.

Advertisement