I see that dptr is an array of double pointers, of size 1, but on construction you seem to be filling in fifty elements. If you want to fill in fifty elements of an array, you need an array of at least size 50.
dptr is an array of double pointers. This means that it is to be filled with double pointers. This
*dptr [i] = (xdata) 0;
is an attempt to put an xdata object (i.e. a double) into the place pointed to by one of those double pointers. That's all well and good, but you never initalised those double pointers so they will be pointing randomly all over your memory space. This should give you an idea of what to try instead:
dptr [i] =new (xdata) 0;
In this way, you are creating a new object of type xdata, and getting the pointer to it (new returns a pointer) and then you are putting that pointer into your array; this is fine, because you created an array of pointers to this type, so what you're putting into the array matches the type of object it is expecting to be an array of.
However, it doesn't make that much sense as a way to create your initial array. Why not make an array of the objects you want to store, rather than an array of pointers to them? How about something like this instead:
1 2 3 4 5
|
dprt = new double[50]; // Create an array of 50 doubles on the heap
for(int i = 0;i < 50; i++)
{
dptr [i] = 0.0; // Set them all to 0.0, just because I like to initialise things.
}
|
where your dptr object would just be:
I note that in your Add_New function, you are trying to create a new object of type xpass. Is xpass, for example, the value 898.9? If so, you can't create an object of type 898.9 as no such type exists.
Also, in that function, you seem to be taking your pointer named dptr, and trying to make it point at the single newly created object. This seems wrong; if you had it pointing to an array before, you've now pointed it somewhere else entirely and can never recover that previous array.
I think you should take a step back, forget about the code, and just think about what you actually want to do. For example, your Add_New functions might look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int Add_New(xdata xpass)
{
// Check that there is enough space to add another object to the array, by comparing
// the size of the array with the number of objects I have already put in it. If it's not
// big enough, make it bigger.
// Now, make the next empty slot equal to the value that was passed in.
// Increment the count I am keeping of how many objects are in the array
// Increment the pointer I am keeping that points at the next empty slot. NB I could
// move the code to check the size of the array and make it bigger to here.
// return some value to indicate that the addition of the value succeeded or failed.
}
|