Memory leaks? Code Optimization?

Started by
12 comments, last by darookie 19 years, 8 months ago
thing is, im not using pointers yet, so this stuff is sort of confusing. i'll refer back to it when i get to pointers though
Silence! I'm Psychoflexing...
Advertisement
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.
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.
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.

This topic is closed to new replies.

Advertisement