File I/O problem for dynamically allocated struct array

This is a discussion on File I/O problem for dynamically allocated struct array within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble reading and writing a dynamically allocated array of structs to a file. I think it has to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    34

    File I/O problem with dynamically allocated struct array

    I'm having trouble reading and writing a dynamically allocated array of structs to a file. I think it has to do with the way I'm allocating memory, because if I statically allocate them (i.e. vector vectout[3]) then it seems to work better, but I still get a debug assertion error with something about an invalid heap.

    With the code the way it is displayed below, the file it outputs has random numbers in it, and I get a debug assertion error about an invalid block type.

    Note: this is just some test code...in the real code, I'm reading the number of vectors from a header, dynamically allocating an array, and then reading them all into the array.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    struct vector
    {
    	float x, y, z;
    };
    
    int main(int argc, char* argv[])
    {
    	vector *vectout;
    	vector *vectin;
    
    	vectout = new vector[3];
    	vectin = new vector[3];
    
    	vectout[0].x = 1.0f; vectout[0].y = 11.0f; vectout[0].z = 111.0f;
    	vectout[1].x = 2.0f; vectout[1].y = 22.0f; vectout[1].z = 222.0f;
    	vectout[2].x = 3.0f; vectout[2].y = 33.0f; vectout[2].z = 333.0f;
    
    	for (int i = 0; i < 3; i++)
    	{
    		cout << "vectout[" << i << "] x: " 
    			<< vectout[i].x << " y: " 
    			<< vectout[i].y << " z: " 
    			<< vectout[i].z << endl;
    	}
    
    	ofstream out;
    	out.open("vector.dat", ios::out | ios::binary);
    	out.write((char *) &vectout, sizeof(vector) * 3);	
    	out.close();
    
    	ifstream in;
    	in.open("vector.dat", ios::in | ios::binary);
    	in.read((char *) &vectin, sizeof(vector) * 3);
    	in.close();
    
    	for (int i = 0; i < 3; i++)
    	{
    		cout << "vectin[" << i << "] x: " 
    			<< vectin[i].x << " y: " 
    			<< vectin[i].y << " z: " 
    			<< vectin[i].z << endl;
    	}
    
    	delete [] vectin;
    	delete [] vectout;
    
    	return 0;
    }
    Any help is appreciated. Thanks.
    Last edited by veecee; 05-05-2006 at 11:45 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Code:
    	out.write((char *) &vectout, sizeof(vector) * 3);
    Remove & before vectout. same with vectin.

    You are creating these dynamically which means that the containers are pointers. Thus using the & is a no no. Since they are already pointers, their name alone will point to the starting address of the object, thus you do not need the & on dynamically created objects.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    34

    Talking

    Man, I feel stupid....I should have known that. That's what I get for using my C++ book's example that used a single struct. Much much appreciated! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 12:39 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21