Would you like to react to this message? Create an account in a few clicks or log in to continue.

How to...Dynamic Memory Allocation

3 posters

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

Go down

How to...Dynamic Memory Allocation Empty How to...Dynamic Memory Allocation

Post  legolizard 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
legolizard
legolizard

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

Back to top Go down

How to...Dynamic Memory Allocation Empty Re: How to...Dynamic Memory Allocation

Post  David B 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!)
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

How to...Dynamic Memory Allocation Empty Re: How to...Dynamic Memory Allocation

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

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

Back to top Go down

How to...Dynamic Memory Allocation Empty Re: How to...Dynamic Memory Allocation

Post  Nighthawk0973 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
Nighthawk0973
Nighthawk0973
Moderator
Moderator

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

Back to top Go down

How to...Dynamic Memory Allocation Empty Re: How to...Dynamic Memory Allocation

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

David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

How to...Dynamic Memory Allocation Empty Re: How to...Dynamic Memory Allocation

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


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

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