Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ dynamic object array using new keyword

Options
  • 18-03-2009 1:33am
    #1
    Registered Users Posts: 695 ✭✭✭


    I had a problem while trying to update an array of objects. The problem was that I had an array of objects, and as the program went on doing stuff the array had to grow to accommodate new objects. The code worked fine for my little testing versions, but now in my project using more real world scenarios it grinds to a halt pretty much as the array can have thousands of elements.

    Is there a better way to do this?

    tempObjectArray is a local method variable
    if (!someBoolean)
    {
        tempObjectArray = new CObjectArray[num];
        
        for (i=0; i<num; i++)
        {
             tempObjectArray[i] = objectArray[i];
        }
    
        delete objectArray;
        objectArray = new CObjectArray[num+1];
    
    
        for (i=0; i<num; i++)
        {
             objectArray[i] = tempObjectArray[i];
        }
    
        objectArray[num].setData(whatever);
    }
    

    Hope the code is not to ambiguous, dont have to code in front of me.
    Tagged:


Comments

  • Registered Users Posts: 26,558 ✭✭✭✭Creamy Goodness


    how about using a vector, you can push and pop objects off and on at ease and vectors can grow and shrink dynamically


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Without seeing your original code and the context in which it's being used it's a bit hard to be sure; correct me if any of my assumptions here are wrong.

    Essentially what your program is doing everytime you want to add a new element to your array is the following;
    1) Create a new temporary array of size num.
    2) Copy EVERYTHING from the old array to the new temporary array.
    3) Delete the old array.
    4) Create a new array of size num + 1.
    5) Copy EVERYTHING from the temporary array back to the new array.
    6) Add the new element.
    

    A much better strategy would be to increase the size of your array by a certain number; even though you may only currently want to add a single new element.

    For example if you increased the size of the array by 1024 every time you have to extend it to store a new element; you would be performing the above steps only once for every additional 1023 elements you wish to add. Currently you're performing the above steps 1024 times to add 1024 elements to your array.

    Hope that makes sense.

    Edit: Indeed as pointed out by Creamy Goodness above there are STL structures which would be better suited to these types of operations; but I assumed this was a College type assignment and it is intended for you to use your own data structures.


  • Closed Accounts Posts: 60 ✭✭LARDO


    i agree with previous advice about ur choice of data structures just being fundamentally wrong , but if u really want to use array u need to look at VirtualAlloc and override the new function, this is fraught!

    this link may be useful http://www.codeproject.com/KB/cpp/vmemorywc.aspx?display=PrintAll

    if we are talking huge arrays that is


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    i'd use STL: vector <CObjectArray>
    then just use resize() method when you want to add or remove.


Advertisement