Resizing an array using pointers not working properly

I’m trying to use a dynamic array to store objects of class “Song”. As user adds/deletes more songs to the playlist the dynamic array should resize itself accordingly. The “add_song()” function should resize the songs array and then add the new song at the end of it.

I’ve thought of using a temp array and first copying everything to temp and then after resizing i add everything back to songs…

class Song{
  private:
    string name;
    int size = 0;
    Song* songs ;
    .....accessors and mutators.
};
class Playlist{
  public:
    void add_song()
        {
            if(size == 0)
            {
                songs= new Song[1];
                songs[0].set_info();
                size++;
            }
            else
            {
                size++;
                Song* temp = new Song[size];
                for(int x=0;x<size-1;x++)
                {
                    temp[x] = songs[x];
                }
                temp[size].set_info();
                delete [] songs;
                songs = new Song[size];
                for(int x = 0; x<size; x++)
                {   
                    songs[x] = temp[x];
                }
                delete [] temp;             
            }
            
        }
};

I cant seem to understand what’s wrong with this code…. The output is fine when i add the first song in the playlist object … after that it doesnt add songs to the playlist obj

  • What “set_info()” does is just set values for the member variable of the Song class

    – 

  • have you heard of std::vector or std::list?

    – 

  • @billz i have but im not allowed to use them

    – 

  • temp[size].set_info(); — This is a memory overwrite. Ask yourself what is the value of size, and what the valid index values you can use in an array of size elements. Also, why do you have two songs = new Song[size]; in the function?

    – 




  • 1

    I am looking at the code you posted. You allocated size elements, and then attempting to call a function on temp[size] — that is totally wrong. It is a memory overwrite or at least a memory access violation, as the valid indices go up to size-1. Your code exhibits undefined behavior as a result of this. Also, it doesn’t matter what the loop you had is doing — that’s not the issue. The issue is what you did after the loop.

    – 




There are several issues with your add_song function:

  1. It access an out-of-bounds item of the dynamically allocated array.
  2. It unnecessarily calls new[] twice.
  3. There is no need to have special code for size == 0.

Here is a rewrite of the function:

  void add_song()
  {
      // allocate size + 1 songs 
      Song* temp = new Song[size + 1];

      // copy songs over to the new array
      for(int x=0;x<size;x++)
          temp[x] = songs[x];

      // set the information for the last song added
      temp[size].set_info();

      // adjust the size to the new amount
      ++size;

     // delete the old array and set songs to the new array
      delete [] songs;
      songs = temp;
   }

Note how the size is incremented after the allocation of size + 1 elements and the setting of the last element in temp using set_info. Your issue is that you incremented size at the very beginning, causing this:

temp[size].set_info();

to access an invalid index.

Leave a Comment