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!
Couple of problems:
The dynamic data is only allocated once (though you have two pointers to the arrea).
SO you can only delete it once (one allocation one destruction).
When copying objects you can not use memcpy (unless the object falls under a very special subcategory). If you are going to use C++ with methods and all its other features this is unlikely to be the case. Prefer to use std::copy() to copy the content from one array to another.
Also why are you copying it twice?
Note in addition to using std::copy
The resize should happen in three distinct phases.
So this is how it should look.
If you are not allowed to use a smart pointer. Then replace phase 1 with this:
Since your class has taken ownership of a dynamic allocated object. Your class should also implement the rule of three. This means you need to define the copy constructor and the assignment operator.
What you really need to have dynamic array are three things:
Here is an example of how to implement the rule of three for an array:
https://stackoverflow.com/a/255744/14065
Anything else is superfluous.
Unnecessary in your case as this is a project. But for extra marks look up how to use placement new. This should prevent extra initialization of objects that don’t really exist in your array.
An example of how to use place new:
https://stackoverflow.com/a/13994853/14065