Sign in to follow this  

Memory leaks? Code Optimization?

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.

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
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[i]=1;
for (int i=0;i<100;i++)
a[i]+i;

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

these are extremely simplified examples but hopefully they get to the point

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

Bas

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.

Hope that helped a bit with your doubts...

Share this post


Link to post
Share on other sites
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 pointer
delete pInt; // Clean up
pInt = 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

Share this post


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

Share this post


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

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

Share this post


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

Share this post


Link to post
Share on other sites
The only time you need to return the memory manually is if it is dynamically allocated (allocated with the new keyword).

char *ptr = new char[1024];  // Dynamically allocate a char array of 1024 elements. This needs to be deleted.
char *another_ptr = new char; // Dynamically allocate a single char, this also needs deleting.

char not_a_ptr; // A normal variable. This gets freed automatically.
char array[1024]; // Same here.


In other words: no, you will probably not need to bother about returning any memory.

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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Quak
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.

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.

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this