fstream with dynamically allocated arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: fstream with dynamically allocated arrays

  1. #1
    Join Date
    Aug 2006
    Posts
    98

    fstream with dynamically allocated arrays

    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!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    Re: fstream with dynamically allocated arrays

    Quote Originally Posted by ejohns85 View Post
    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 is a pointer to a float. It is not a "dynamic array". So what is
    Code:
    sizeof(float*)
    That is your problem.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,423

    Re: fstream with dynamically allocated arrays

    In addtion, dynamic arrays are easily accomplished in C++ by using std::vector. Your code as it is posted, has memory leaks, let alone incorrect usage of sizeof().
    Code:
    #include <vector>
    //...
    std::vector<float> x(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[0], sizeof(float) * x.size());
    out.close();
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Aug 2006
    Posts
    98

    Re: fstream with dynamically allocated arrays

    Ok thanks, I understand that now. However, I'm now confused why my attempt with the static array worked. If I used "float x[3]" instead of "float* x = new float[3]", with "out.write((char*)x, sizeof(x))", then why does "sizeof(x)" make sense? I thought that if you declare a static array x, then x actually represents a pointer to the start of the array, rather than the value of the first element. Therefore, "sizeof(x)" is asking for the size of the pointer, not the size of the whole array. Why should this then work?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: fstream with dynamically allocated arrays

    Quote Originally Posted by ejohns85 View Post
    Ok thanks, I understand that now. However, I'm now confused why my attempt with the static array worked. If I used "float x[3]" instead of "float* x = new float[3]", with "out.write((char*)x, sizeof(x))", then why does "sizeof(x)" make sense? I thought that if you declare a static array x, then x actually represents a pointer to the start of the array,
    Arrays are not pointers, and pointers are not arrays. An array is a type, a pointer is a type, they are not the same thing.
    Code:
    int x[3];  // this is of type array of 3 ints
    int *px;  // this is of type pointer to int.
    Secondly, sizeof() is a compile-time value. It cannot be used to determine run-time sizes.
    Code:
    cout << "Input the number of value: ";
    int n;
    cin >> n;
    float* x = new float[n];
    So how is sizeof() going to know what "n" is?
    I thought that if you declare a static array x, then x actually represents a pointer to the start of the array,
    No, x is an array if you declare an array. See above.

    What you are confused about is that an array, if you pass it to a function, decays to a pointer. That is a totally different situation than if you declare an array, declare a pointer, and believe they are the same thing (when they are not).

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center