Hello. I have a class called Book and I am trying to create a dynamic pointer based array of the class. When I try to run the program I keep getting the error: pointer being freed was not allocated at the line of code that says "delete [] A;". I am using Xcode to run the program. Does anyone know what I am doing wrong?
Book *changeArraySize(Book *A, int &size, double factor)
{
int i;
int newsize = size*factor;
Book *A2 = new Book[newsize];
for(i = 0; i < size; i++){
*(A2+1) = *(A+1);}
size = newsize;
delete [] A;
A = NULL;
return A2;
}
Sorry! that *(A2+1) = *(A+1) should be *(A2+i) = *(A+i) so that so the elements from the original array are copied to the new array (I changed that part of the code after I posted my question) before A is deleted.
I included A = NULL so the pointer wouldn't be dangling