Deleting and resizing a pointer array.

This is a discussion on Deleting and resizing a pointer array. within the C++ Programming forums, part of the General Programming Boards category; I have a game, and i would like to be able to resize the array of enemies for every level, ...

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    Deleting and resizing a pointer array.

    I have a game, and i would like to be able to resize the array of enemies for every level, right now the array is just set to a static 100, but that only allows 25 levels. I'm not looking for more level's, rather, more dynamic memory usage.

    something like; (note: this causes a segmentation error on my compiler)
    Code:
    Enemy enemyArray[NumberOfEnemies];
    .....
    //New Level
    
    NumberOfEnemies+=2;
    delete [] enemyArray;
    Enemy enemyArray[NumberOfEnemies];
    .....
    //ect...
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    use a vector

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    you cannot deallocate something you allocated on the stack, hence the segfault.

    Code:
    Enemy enemyArray[NumberOfEnemies];
    ...
    delete [] enemyArray;
    is not allowed.

    There is no easy way to do it with new/delete. You have two options, use a std::vector, or malloc/realloc/dealloc. The latter is not recommended in C++ (but will work), since it's from C.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    there is a third option:
    Code:
    template <class T> T* arrayResize(T arr[],const unsigned int oldSize,const unsigned int newSize, T defaultVal)
    {
            T *temp = new T[newSize];
            unsigned int i;
            if(oldSize<newSize)
            {
                    i=oldSize;
            }
            else
            {
                    i=newSize;
            }
            while(i)
            {
                    --i;
                    temp[i]=arr[i];
            }
            delete[] arr;
            i=oldSize;
            while(i<newSize)
            {
                    temp[i]=defaultVal;
                    ++i;
            }
            arr = temp;
            return arr;
    }

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by cyberfish View Post
    There is no easy way to do it with new/delete. You have two options, use a std::vector, or malloc/realloc/dealloc. The latter is not recommended in C++ (but will work), since it's from C.
    Only if you're using PODs.
    If you're allocating objects, you have to use new/delete, otherwise the constructors/destructors won't be called.

    vectors & strings are your friends. Use them as much as possible to make your life a lot easier.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    you have basically reinvented a harder to use and less efficient version of std::vector.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    there is a third option:
    However, it does illustrate why using a vector as you suggested would be better - the temporary array is default initialised, but it would be better to defer the initialisation, say with placement new, but this would then complicate the use of the dynamically allocated array. Also, oldSize and newSize should be of type size_t, with the const qualifier not useful in this case. defaultVal should probably be passed by const reference.

    Incidentally, I would think that for loops are more appropriate here
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    thanks. illustrative as always.

    i read that article you linked about using placement new. it seemed simple enough, but IIRC the char buffers had to be freed/deleted explicitly in addition to deleting the object that was allocated on that memory space. so in a resizing function such as this, rather than a container class, it didn't seem practical to track all the char buffers that were allocated.

    yes, i realize this is more an argument for the use of container classes (namely vector) than anything

    if i've grossly misconstrued something here, please let me know...

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    just looked into it and in my compiler size_t is identical to unsigned int

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    just looked into it and in my compiler size_t is identical to unsigned int
    But you will probably also find:
    1. Some other architectures have a size_t that is not the same size as int. [1]
    2. size_t is a unsigned value, int is signed.


    [1] Generally, size_t is the same size as a pointer - not ALWAYS, but most of the time.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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