Recommended Posts

ok, so obviously, im just getting started with C++ programming. i havent gone any further than console apps at this point, so i havent run into pretty much any problems. so i was wondering what types of things cause "memory leaks"? and while we're at it, what the heck is a memory leak?? also, when someone says they need to do some "code optimization" what does this mean, and could i get some sort of example?? thanks guys

Share this post

Link to post

Share on other sites

When you encounter a memory leak you allocated memory but you didn't release it at the end of your program. So when you program isn't running any more your allocated memory is still there and no other program can overwrite that memory. The memory will only be deleted when you restart your computer.

Code optimization: Just what it says optimizing your code, make it better, faster, or whatever

Share this post

Link to post

Share on other sites

A "Memory Leak" is a piece of memory that your program requested from the Operating System, but neglected to return it back, on exit.

Imagine an innocent-looking function that requests a small ammount of memory (lets say 20Kb), but never gives it back. Now imagine that this function runs 50 times per second. After the first second, almost 1000Kb, 1Mb have been requested. The OS only knows the app requested it, it cant guess it is no longer in use. After the first minute, your app has "eaten" 60Mb. It will only take 9 minutes to consume more than 512Mb of Ram, therefore MemLeaks need to be traced, and all memory must be returned to the OS when not in use.

Code Optimizations... and optimization is a word very similar to "better", and better is something that is relative to every person. Some programmer prefer their code stable and eliable, others prefer it blazingly fast (im not saying both are mutually exclusive... but sometimes they appear so).

So in those situations, optimizing the code might either mean, add more error checks, and error recovery procedures, or, create look-up tables and resort to other "magic tricks" to make it go faster and faster.

No, if you forget the delete for some reason, but do set the pointer to NULL, the pointer is LOST. It's still on the heap, but you can no longer reach it. This is called a memory leak. You allocate memory, but never clean it up. If you do it all the time, your application runs out of memory and crashes.

A memory leak only happens if you use pointers. Normal variables will be cleaned up when the stack unwinds(If you leave a function, the stack unwinds back to the function that called the function that is being unwinded, and all the variables on the stack will be cleaned aswell).

Code optimization is based on SPEED. If you write a game, you generally just type code that works. But in some cases, the code is horribly slow and your game doesn't run very well. Using optimization you speed up code(There are million techniques and each technique is used in certain situations). Thus, optimization. Don't bother with optimization unless you are sure the game runs at a bad FPS and be sure to run an profiler so see what code is slow.

Share this post

Link to post

Share on other sites

Original post by GraphicsBasWhen you encounter a memory leak you allocated memory but you didn't release it at the end of your program. So when you program isn't running any more your allocated memory is still there and no other program can overwrite that memory. The memory will only be deleted when you restart your computer.

Unless you are still running an archaic OS like Windows 9x, this is not true. Memory leaks are basically only affecting your programs runtime behavior, e.g. you are more likely to run out of physical RAM. Any modern OS will release all memory that was acquired during program runtime (this might, however, not occur ad-hoc after the process terminates). Memory leaks are neverless quite bad, as they also cause dynamic (heap-) memory fragmentation. This will degrade memory allocation performance.

An exception to this are system resources such as handles to OS objects. These are usually a very rare resource and your program is likely to run into trouble if you are not releasing them.

Share this post

Link to post

Share on other sites

so when i just run programs simple as displaying text to the screen, and taking input from the user, is it taking memory? and if so, does it delete at the end of the program? and if it doesnt, how do i return the memory

0

Share this post

Link to post

Share on other sites

If you have allocated memory likechar* ptr;ptr=new char[100];then you return or release the memory likedelete ptr;BEFOR the function you allocated the memory in will be removed from the stack! Othewise your pointer, which you itialized in the function will be removed, too.A good thing to do is, if you delete memory, to set your pointer aftewards to NULL likeptr = NULL;So your programm won't crash if you use the pointer later by accident.Quak

Share this post

Link to post

Share on other sites

If you are not pointers yet you needn't worry about memory leaks for the moment, because without pointers you can't allocate dynamic memory and won't be troubled by leaks. That's why other languages like Java and (partly) C# don't use pointers at all.

0

Share this post

Link to post

Share on other sites

Original post by QuakIf you are not pointers yet you needn't worry about memory leaks for the moment, because without pointers you can't allocate dynamic memory and won't be troubled by leaks. That's why other languages like Java and (partly) C# don't use pointers at all.

Side note: Insterestingly enough you can still have memory Leaks in Java programs [smile]But as Valderman and Quak already mentioned - in C/C++: no new/malloc, no memory leaks [smile]

Make yourself familiar with pointers anyway, you will need them once you want to use more memory. Stack memory (that's what you currently use) is limited so you cannot simply declare a

int [2000000][2];

even though you have lots of RAM.

0

Share this post

Link to post

Share on other sites

This topic is 4865 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.