Hello,

I have a dynamically allocated array, x, which I want to save to file and then read back again. I have tried this with a static array and it works fine. However, I cannot get the desired results with a dynamic array. Here is my code:

Code:
float* x = new float[3];
x[0] = 1.0;
x[1] = 2.0;
x[2] = 3.0;
ofstream out("data.dat", ios::out | ios::binary | ios::trunc); 
out.write((char*)x, sizeof(x)); 
out.close(); 

float* y = new float[3];
ifstream in("data.dat", ios::in | ios::binary); 
in.read((char *) y, sizeof(y)); 
 
for(int i = 0; i < 5; i++)
cout << y[i] << " "; 
 
in.close();
This only reads in the correct value for the first element of the array. The rest are not assigned to. I have also tried using "3 * sizeof(y))" instead because I thought that this would read the full length of the array back, but it still doesn't work.

Any ideas? Thanks!