Dynamic memory (new && delete)

Dynamic memory (new && delete)

Post by L0DER on Fri Oct 10, 2008 12:40 pm
([msg=13486]see Dynamic memory (new && delete)[/msg])

A few days ago i read about dynamic memory, and I didn't understand much could someone give me a hand?
I didn't understand what are they, what they good for.
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: Dynamic memory (new && delete)

Post by BhaaL on Fri Oct 10, 2008 4:41 pm
([msg=13500]see Re: Dynamic memory (new && delete)[/msg])

In case you know some C, they are equivalent to malloc and free.
new is used to allocate memory, delete is used to free it again.

Code: Select all
char sbuffer[20]; //static allocation
char *dbuffer = new char[20]; //dynamic allocation
delete dbuffer; //dont forget to free it; else it leaks memory


Why is this important/useful?
Ever tried this?
Code: Select all
int size = 20;
cin >> size;
char sbuffer[size]; //error, size is not known at compile time
char *dbuffer = new char[size]; //allocated as runtime, where size is known


There are other uses and reasons for dynamic allocation, but you should be able to get somewhere with that knowledge.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Dynamic memory (new && delete)

Post by L0DER on Sat Oct 11, 2008 1:20 am
([msg=13511]see Re: Dynamic memory (new && delete)[/msg])

well, i didn't understand it :cry: , but i tried the code and it compiled without errors.(but didn't worked properly ;) )
what does it help you if you allocated the memory dynamically :?:
why couldn't you be pleased just with: :?:
Code: Select all
define MAX_SIZE 20
// or
int max_size = 20;
/////////////////
char chArray[MAX_SIZE/* OR */ max_size];
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: Dynamic memory (new && delete)

Post by BhaaL on Sat Oct 11, 2008 5:38 am
([msg=13515]see Re: Dynamic memory (new && delete)[/msg])

The define way is static allocation, and only helps you organize your constants. Imagine you'd need 40 now instead of 20, you just change MAX_SIZE to 40, rather than looking through your code where you use the number 20.

The second way however does *not* work, because max_size is not constant.

The main idea behind dynamic allocation is optimized memory usage. Having char buffer[200] uses 200 Bytes, no matter if you need them or not. char *buffer uses 4 Bytes, and you can allocate as much as you need - when you need it.
There are still systems out there that have few to no memory, and you have to use it wisely (I just recently talked to a guy that wanted to run a game of his on a calculator with 118 KB Memory - asking a similar thing about malloc/free).

Dynamic allocation makes more sense with Data structures tho. Grab a struct that holds a pointer to its next element, allocate them with malloc when you need them.
Create vectors of objects, in-time when required - no limitation to size on compile time. Need one more, just add another.
Doesnt work with static sized arrays.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Dynamic memory (new && delete)

Post by L0DER on Sat Oct 11, 2008 6:25 am
([msg=13517]see Re: Dynamic memory (new && delete)[/msg])

Thanks you. Now I understand.
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests