I'm currently sitting with an assignment in which you are supposed to read spheres from a file. A typhical input from the file can look like this: "0.182361 -0.017057 0.129582 0.001816". Representing the, x, y and z coordinates plus the Spheres radius. While reading the File I'm using my own method: "AddSphere" which adds a sphere into an array.
void Array::AddSphere(MySphere inp)
{
if (length == 10000 || length == 200000 || length == 30000)
{
ResizeBuffer();
}
this->length++;
*arr = inp;
arr++;
//this->length++;
}
The class "Array" is supposed to be like a holder for all spheres and is containing the variable "arr" that is pointing on the current element.
class Array
{
public :
int length;
void AddSphere(MySphere inp);
int arrLength();
void RemoveAt(int pos);
void AddFromFile();
MySphere * Buffer;
void CreatenewBuffer();
private:
MySphere * arr;
public:Array()
{
Buffer = new MySphere[10000];
arr = Buffer;
length = 0;
}
~Array()
{
delete[] arr;
delete[] Buffer;
}
};
It is also containing "Buffer" that is pointing on the first element in "arr". So now what is my problem? The problem is that i want to be able to dynamicly increase the Buffer's size whenever "length" equals a specified value. Lets say my file contains 15000 elements. Then i need to increase the Buffers size to be able to have the correct length on the array.with ResizeBuffer() i try to do that.
void Array::ResizeBuffer()
{
MySphere * tempBuff = new MySphere[length+10000];
memcpy(tempBuff, Buffer, sizeof((MySphere)*Buffer));
this->Buffer = new MySphere[length+10000];
arr = Buffer;
memcpy(Buffer, tempBuff, sizeof((MySphere)*tempBuff));
//int a = 0;
}
But for some reason i only get the last 5000 elements in my output instead of all the 15000. I figured that it has something to do with the pointer of arr not being pointed to the whole buffer, but none of my attempts work. So why is this happening?
Thank you for your time!