Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AndreTheGiant

Is this a mem leak?

This topic is 5529 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
In most cases the OS will clean your memory, but that''s VERY bad coding practice.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!