How to...Dynamic Memory Allocation

 :: C Languages :: C++ :: Help!

View previous topic View next topic Go down

How to...Dynamic Memory Allocation

Post  legolizard on Sun Oct 02, 2011 12:28 am

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


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. Razz) 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
avatar
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

View user profile

Back to top Go down

Re: How to...Dynamic Memory Allocation

Post  David B on Sun Oct 02, 2011 12:29 am

Thanks for making this! (I didn't read it, but I glanced at it, and it looked like it is very good!)

_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: How to...Dynamic Memory Allocation

Post  Nighthawk0973 on Sun Oct 02, 2011 2:50 pm

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...
avatar
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 19
Location : In Front of My Computer

View user profile

Back to top Go down

Re: How to...Dynamic Memory Allocation

Post  Nighthawk0973 on Wed Oct 12, 2011 8:56 pm

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

_________________
The Grid:
I tried to image clusters of information as they moved through the computer.
Were they jets, or motorcycles? Were the circuts like freeways? I kept dreaming of a world I thought I'd never see. And then one day... I got in.
avatar
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 19
Location : In Front of My Computer

View user profile

Back to top Go down

Re: How to...Dynamic Memory Allocation

Post  David B on Wed Oct 12, 2011 9:31 pm


_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: How to...Dynamic Memory Allocation

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 :: C Languages :: C++ :: Help!

 
Permissions in this forum:
You cannot reply to topics in this forum