Memory leaks? Code Optimization?

Started by
12 comments, last by darookie 19 years, 8 months ago
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
Silence! I'm Psychoflexing...
Advertisement
A memory leak in the strictest sense is allocating some memory and not freeing thus "leaking" memory.

i.e.
main()
{
while(1)
{
char* something=new char[200];
}
}

That will eventually eat all the memory on your machine ( very bad don't try it )

Code optimisation is fundementally going through for code making it run faster, finding quicker way to calculate stuff finding faster code paths
i.e.

Slow code
for (int i=0;i<100;i++)
a=1;
for (int i=0;i<100;i++)
a+i;

Faster code
for (int i=0;i<100;i++)
a=1+i;

these are extremely simplified examples but hopefully they get to the point
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

Bas
GBS
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.

Hope that helped a bit with your doubts...
A memory leak is a piece of memory that gets lost because you do not clean it up. I assume you have a grasp of pointers. If you create a new pointer, you do this:

int *pInt = new int;  // Create new pointerdelete pInt;  // Clean uppInt = NULL; 


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.

Toolmaker

ok, sounds very good in theory... yet i still have no idea how to do this stuff, lol. got any links for excercises or anything that could help me? or just tell me, how do i "return" the memory??
Silence! I'm Psychoflexing...
Quote:Original post by GraphicsBas
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.

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.

Cheers,
Pat.
You return memory to the OS by simply doing delete ptr;, or delete [] array; if you have a dynamically allocated array instead of a single object.
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
Silence! I'm Psychoflexing...
If you have allocated memory like
char* ptr;
ptr=new char[100];
then you return or release the memory like
delete 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 like
ptr = NULL;
So your programm won't crash if you use the pointer later by accident.
Quak

This topic is closed to new replies.

Advertisement