Dynamic Arrays

This is a discussion on Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; Is there a way to create dynamic arrays in C++?? If so.. how do i use them? A simple code ...

  1. #1
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001
    Posts
    26

    Red face Dynamic Arrays

    Is there a way to create dynamic arrays in C++?? If so.. how do i use them? A simple code example would be great.

    Thanx, HeadExplodes.
    /* MSN and E-Mail - head_explodeshotmail.com */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Jupp there is, with pointers. It could look something like this.

    #include <iostream.h>

    int main()
    {
    int *pInt = new int[10];
    int *pIncre = pInt;
    for (int i = 0; i < 10; i++)
    {
    *pIncre = 2*i;
    pIncre++; // "jumps" to the next element
    }
    pIncre = pInt;
    for (int i = 0; i < 10; i++)
    {
    cout << "Value pInt[" << i << "] is " << *pIncre << endl;
    pIncre++;
    }

    delete [] pInt; //free memory(heap)
    //dont use delete pInt because it only frees the
    //fist element and you get a memory leak :-(
    return 0;
    }

    This should work good (I havent test it yet). Please reply and tell how it went.

  3. #3
    Unregistered
    Guest
    Jupp there is, with pointers. It could look something like this.

    #include <iostream.h>

    int main()
    {
    int *pInt = new int[10];
    int *pIncre = pInt;
    for (int i = 0; i < 10; i++)
    {
    *pIncre = 2*i;
    pIncre++; // "jumps" to the next element
    }
    pIncre = pInt;
    for (int i = 0; i < 10; i++)
    {
    cout << "Value pInt[" << i << "] is " << *pIncre << endl;
    pIncre++;
    }

    delete [] pInt; //free memory(heap)
    //dont use delete pInt because it only frees the
    //fist element and you get a memory leak :-(
    return 0;
    }

    This should work good (I havent test it yet). Please reply and tell how it went.

  4. #4
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Or a simpler way to assign values to array in this case would be:

    for (int i = 0; i < 10; i++) pInt[i] = i*2;

    This does exactly same thing as:

    for (int i = 0; i < 10; i++)
    {
    *pIncre = 2*i;
    pIncre++;
    }
    Making error is human, but for messing things thoroughly it takes a computer

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I totaly agree Kitten. Using vector is better in this case
    (I just didnt want to mix vectors and pointers).

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Okay, all of that stuff is hard coding, people. Arrays are naturally dynamic, because arrays are pointers. I highly suggest not using a pre-made vector class because you will not always have that vector class on you. Use your own knowledge.
    My Website

    "Circular logic is good because it is."

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I don't see any vectors, where?

    Arrays are not naturally dynamic. An array has local scope, it is allocated on the function stack, and has to be passed into functions as an arguement, but it is passed as a pointer parameter. To dynamically allocate an array, use the 'new' function, than the array will be stored in 'heap' memory. This means that if the array is dynamically allocated in a called function, when the function terminates the memory on the heap will be reserved and can be passed safely back to the caller. On the other hand, if you define an automatic array in a function and try to pass it back to the caller, that memory will become unreserved because it was allocated on the function stack. When the function ends the stack memory is discared into the free store.
    Last edited by Witch_King; 08-17-2001 at 11:36 AM.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    God forbid one uses standard classes, for if they must use a compiler that is 5 years old, they might not be supported.

    Code re-use should be encourged IMO. No need to reinvent the wheel.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  9. #9
    Lowas
    Guest
    Use the vector class.
    Fooling around with different data structures like arrays and linked lists etc is very good for learning but when it's time to do something serious you should use what's in the standard library.

  10. #10
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564

    Lowas

    // And btw...a little example of using the vector class
    // and it's not hard to use either

    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<int> v;

    while ( 1 )
    {
    cout << "Enter a number (negative to quit)\n";
    int temp = 0;
    cin >> temp;
    if ( temp < 0 ) break;
    v.push_back(temp);
    }

    for ( int i = 0; i < v.size(); ++i )
    {
    cout << v[i] << ' ';
    }
    v.clear();
    return 0;
    }
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  11. #11
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Hey somebody's posing as me....
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  12. #12
    Lowas
    Guest
    I don't know how that happend but I'm not taylorguitarman =P.

  13. #13
    Lowas
    Guest
    Probably a bug tayloyrman, I didn't post as you on purpose anyway.

  14. #14
    Unregistered
    Guest
    Testing.

  15. #15
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    Well, when i tried to login a while ago i was mysteriously logged in already as someone who i wasn't.

    Also, when i tried to register, it logged me in as someone who i wasnt.

    Very strange, very strange indeed.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 12:32 PM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21