Deleting Dynamically Allocated Array Members

This is a discussion on Deleting Dynamically Allocated Array Members within the C++ Programming forums, part of the General Programming Boards category; I have made a program that introduces a class, Inventory, that holds a given number of items introduced by the ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23

    Deleting Dynamically Allocated Array Members

    I have made a program that introduces a class, Inventory, that holds a given number of items introduced by the constructor. Observe:

    Code:
    class Inventory
    {
    public:
        Inventory()
        {
            cItems=new Item[defaultSize];
            suiCap=defaultSize;
        }
        Inventory(short unsigned int suiSize)
        {
            cItems=new Item[suiSize];
            suiCap=suiSize;
        }
        ~Inventory()
        {
            delete [] cItems;
        }
        Remove(short unsigned int suiIndex)
        {
            delete cItems[suiIndex];
        }
    protected:
        Item* cItems;
        short unsigned int suiCap;
    private:
        static short unsigned int siDefaultSize;
    };
    static int Inventory::siDefaultSize=32;
    Inventory myInv(10);
    What I am aiming to do is delete an individual member of the array. Is this possible? I have tried to delete it through my "Remove" function but...:
    Code:
    myInv.Remove(5);
    This gives me compiler errors, saying that Item * cannot be converted to void *.

    So, any workaround to this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    In C++ you should be using a vector instead of dynamic arrays. The vector class has an erase function that allows you to erase an element at a specific index.

    If you decide to keep using C style arrays, then you must manually remove the element at that index from the array either by moving each element up that comes after it, or copying the entire array except for the item to be removed to a new array. You don't want to delete anything because that frees memory allocated by new and the only memory you allocated was the memory for the entire array all at once.

    Also, if you don't use vectors, you'll probably need a copy constructor and copy assignment operator for that class.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    The problem is we are designing a game and we need a fast method of removing and adding items. And I have heard that vectors are very slow. Up until now I have been making due by changing the amount (items in stack count) of the item to -1.

    Of course, -1 signifies the abscence of an item.
    0 is not appropriate because 0 is used to identify an item that takes up a whole slot, whereas numbers above that identify that the item is a stack.
    Last edited by theJ89; 03-26-2006 at 06:36 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Then a dynamic array is probably not what you want to use. What you need depends on many variables, like whether you require random access (ability to access a specific item by its index in the data structure), whether the additions and removals will occur only at the back or front of the container, or if they can be from the middle (like your example), whether the Item's themselves have specific properties that make them unique and can be sorted, and many other factors.

    Based on the information you've given, a deque is probably your best solution right now.

    Edit: based on your added information, using -1 to signify absence might be a good idea, as it avoids more allocations, but if you are continually adding items, then that would cause the allocations itself and you would lose the benefit. Standard containers are usually not slower than C style containers, especially if you aren't using the C style containers appropriately. I think it would be wiser to start with the C++ container that does the work for you than to try to implement these things yourself. As your code demonstrates, it is not easy, and the C++ containers have done a lot of the work for you.
    Last edited by Daved; 03-26-2006 at 06:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. Dynamically create an array?
    By Unknowntoyou000 in forum C++ Programming
    Replies: 16
    Last Post: 04-25-2008, 03:49 PM
  4. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  5. Run-time error with dynamically allocated 3-D array
    By OceanDesigner in forum C Programming
    Replies: 2
    Last Post: 10-21-2005, 02:29 PM

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