C++ 106 For loops and Dynamic Arrays

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
11,451
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Mar 8, 2008

2 things to note:
the int i is only in the scope of that for loop
and
if you need to free up the memory that you have alotted with "new" you call "delete" so if it's a point to 1 object you would call:
delete pointer;
if it's a pointer to an array you do:
delete [] pointer;

let me know if anything else needs to be cleared up^^

Category:

Howto & Style

Tags:

License:

Standard YouTube License

  • likes, 4 dislikes

Link to this comment:

Share to:

Uploader Comments (ryutenchi)

  • So how do we resize dynamic arrays, keeping the old values?

  • you have to use a temp. pointer then you can set the array to a new Whatever[newsize]; and copy the values over from the temp point. I know it's a bit complex but there is no build-in method so that's the best way to do it with this method. Alternatively, you can use the *allac C commands to create/size/resize a pointer and then delete them with the free command.

  • i hope u understand my question here it goes:

    I set an array for example:

    int number[1]; the i set number[0]=100, number[1]=200.

    Is there any way to change number[0] to different number while running the program?

    if so let me know

    thx

  • First, "int number[1];" will only have 1 "slot" in it, so "number[1]=200;" will through an error.

    Second, it is only number of items in an array that you can not change(and that's not always true, but is out of scope). You can change the values at any point so

    number[0]=100;

    if(blue) number[0] = 42;

    else number[0] = 73;

    is perfectly valid. hope that helps.

  • Under tutorial about polimorphism I asked in comment about pointers and what made me confused was that "new" word. So:

    character* me; // pointer to an adress in memory

    me = new Player();

    Last line makes compiler to look at the constructor "Player()" of the "character" class and then write a NEW set of arguments stored in this constructor into the memory at the adress pointed by "me".

    You've created an instance of the "character" class but straight into the memory at the "me" adress :).

  • Not quiet.The character class pointer"me"is set up as a generic character pointer that can be used to access any of the public functions or variables of a character,but since we then make an instance of Player and set it equal to it, it now is actually a player,but we only have access to the functions and variables that were inherited or redefined from the base class(character).So polymorphism lets me make a base class pointer and then point it at an object of a derived class at anytime.

see all

All Comments (20)

Sign In or Sign Up now to post a comment!
  • @ryutenchi wow, this was quite some time ago. I passed my C++ course, lol. Thanks!

  • what happened to the delete part with out it its gonna give massive excpetion

  • You know, from a coding perspective this is really tedious to go through the entire dynamic array process every time one is needed- why not just add a library/function for it?

  • @t3hdewd Use vecotrs or just malloc away.

  • hi.... where did you download ur type of C++ programming from?

    i have a 64 bit computer and it wont let me run a c++ program..it says it only runs in 32 bit computerss :(

  • Sorry, Didnt mean to make my last comment sound that way. i just re-read it.

    What i mean is the other video sticks on topic, and doesnt cloud the new guy with additional information such as a FOR loop.

    it actually TEACHES dynamic memory, as an advanced topic expecting basic class/loop knowledge already "as probably should when covering a more advanced topic like dynamic memory use"

  • Yes, I figured out that in the start, but I had some mistake and I though there is another way, but it seems I was right Thanks ;-)

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more