Copying a pointer's items to another pointer

So I have these instructions:
"
If there is NOT enough space
Set m_space to m_space + 5
Allocate a new pointer to an integer with m_space elements.
Copy the old pointer items into the new pointer items
Delete the old pointer items
Set m_items to point to the new pointer items
"

My code doesn't work, and I was wondering how to go about doing this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
else
   {
      m_space +=5;
      int *pointer = new int[m_space];
      for (int i =0; i < m_space; i++)
      {
      
   
         pointer[i] = m_items[i];
         m_items[i] = 0;
         m_items[i] = pointer[i]; 
         

       
         
         
      }
}


The code compiles, but it doesn't work. Any help?
line 10 is not how you delete a pointer. Use delete [] m_items;

I'm guessing m_space is an int representing the size of the array m_items? If so, then let's say the size of m_items before this else statement is 22. Inside the else statement it is increased to 27. You then try to loop through 27 indexes of m_items, which only has 22.
Last edited on
Topic archived. No new replies allowed.