How to...Dynamic Memory Allocation
3 posters
:: C Languages :: C++ :: Help!
Page 1 of 1
How to...Dynamic Memory Allocation
Hello everyone. Today we are going to discuss a very importane aspect of programming; dynamic memory allocation. For all those Java guys out there this WILL seem just like home. :-)
Scenario :
We have an array of integers of size 5 like so...
So in this example our array is always going to be size 5. Every single time we run our pogram with this statment the array will always be 5.
However, what if, we did not want our array to be always 5 what if we need to get input from the user and enters in too much c**p thus overloading the array?
This is when dynamic memory allocation comes to save the day. Note : DYNAMIC.
So lets re-write our little program...
Don't worry I will explain the "???????????" in a minute. What we are basically saying is...
Hey computer go to this place right HERE(points to place in memory) and make a new array of integers of x size. Note : X amount a VARIABLE or in other words it is DYNAMIC not STATIC.
There is one problem however. Our programming is leaking...memory. :O Unlike Java, which is a garbage collecting language(I bet you thought I was going to say garbage language. ) in C++ we need to explicitly delete dynamically allocated memory. We do this with either the keyword delete or delete []
So....
And there we go. Any other questions I will be happy to answer.
Scenario :
We have an array of integers of size 5 like so...
- Code:
int array [5] = {...};
So in this example our array is always going to be size 5. Every single time we run our pogram with this statment the array will always be 5.
However, what if, we did not want our array to be always 5 what if we need to get input from the user and enters in too much c**p thus overloading the array?
This is when dynamic memory allocation comes to save the day. Note : DYNAMIC.
So lets re-write our little program...
- Code:
int howBig;//Basic integer
cout << "Enter in how big you want the array to be..." << endl;//Basic cout statment.
cin >> howBig;//Basic cin statment
int *apnter = new int [howBig];//???????????
Don't worry I will explain the "???????????" in a minute. What we are basically saying is...
Hey computer go to this place right HERE(points to place in memory) and make a new array of integers of x size. Note : X amount a VARIABLE or in other words it is DYNAMIC not STATIC.
There is one problem however. Our programming is leaking...memory. :O Unlike Java, which is a garbage collecting language(I bet you thought I was going to say garbage language. ) in C++ we need to explicitly delete dynamically allocated memory. We do this with either the keyword delete or delete []
So....
- Code:
int howBig;//Basic integer
cout << "Enter in how big you want the array to be..." << endl;//Basic cout statment.
cin >> howBig;//Basic cin statment
int *apnter = new int [howBig];
delete [] apnter;//Note how I did NOT use the derefernece operator.
And there we go. Any other questions I will be happy to answer.
Last edited by legolizard on Sun Oct 02, 2011 12:32 am; edited 1 time in total
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: How to...Dynamic Memory Allocation
Thanks for making this! (I didn't read it, but I glanced at it, and it looked like it is very good!)
Re: How to...Dynamic Memory Allocation
I was all for it, but than you did something that looked like making a C++ pointer, and I said "um.... just go done with school, brain needs a break!"
Naturally I than went to go boggle my mind off making my MMORPG in blitz...
Naturally I than went to go boggle my mind off making my MMORPG in blitz...
Nighthawk0973- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: How to...Dynamic Memory Allocation
Yes, it's partly my fault, which is why I'm fixing it: this topic is getting out of hand.
Topic has been Locked by Nighthawk0973 Wednesday, October 12th, 5:56 Mountain Time
Topic has been Locked by Nighthawk0973 Wednesday, October 12th, 5:56 Mountain Time
Nighthawk0973- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: How to...Dynamic Memory Allocation
Spam removed (https://programmingforums.forumotion.com/t118-dynamic-memory-allocation-spam-posts). Thread unlocked.
:: C Languages :: C++ :: Help!
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|