0

So for a school assignment I was writing a program that uses dynamic 2D 'arrays' to model matrices and perform operations on them. So what I did was create a structure called matrix which contains two integers for the rows and columns, and a double pointer for the 'array'. I initialized the pointers like this:

int **ary=new int*[row];
for(int i=0;i<row;++i)
     ary[i]=new int[col];

Now I know that my program is going to look horribly slow and inefficient and generally horrible to people on this site, but just keep in mind that I am doing a school assignment and I can't use fancy things like vectors and all.

Then I tried to write and then read the structure objects in a binary file like using an fstream object y like this:

matrix M;
y.write((char*)&M,sizeof M);
y.read((char*)&M,sizeof M);

The program worked, but what I entered was different from what the file read. Why is that so? And what are other methods (that use fstream) to store my matrices in a file?

NOTE: As is evident, I am completely new to this site, and as of yet I do not know the proper, um, etiquette of asking questions on this site. If my question seems weird in some way or is missing some information, please leave a comment. Constructive edits are welcome.

13
  • What is M? Please provide MCVE. Commented Dec 7, 2015 at 14:48
  • 2
    Hint: saving pointers to file doesn't make sense. You should write each values instead of pointers. Commented Dec 7, 2015 at 14:49
  • 3
    C++ is not Java. Stop newing everything you create. We have std::vector, std::array, and language-level arrays. You don't have to new every object you use. Commented Dec 7, 2015 at 14:49
  • 1
    @G-man Save the size of the matrix before saving each elements. Commented Dec 7, 2015 at 14:55
  • 1
    @G-man If you want to know why your routine could never have worked, the value of sizeof M in your read and write calls are compile-time value. Let's say that sizeof M is 50 bytes. That 50 doesn't change value, no matter what the dimensions of your matrix are. It could be 1 x 1 or 10000 x 10000. Given that information, even if you didn't know the ins and outs of pointers, you see that there is no way saving in the way you've done could have worked. Commented Dec 7, 2015 at 15:15

2 Answers 2

2

You can do that if you allocate all the memory in one single block (See the other answer(s) if you don't want to do that):

int* block = new int[row*col]
for (int i=0; i < row; i++)
   ary[i] = block + i*col;

Basically I'm allocating a single large array then make each row point to the corresponding portion of the array. Now if you write

y.write(static_cast<char*>(block), sizeof(int) * row * col);
y.read(static_cast<char*>(block), sizeof(int) * row * col);

you will get the same thing out. It's probably now obvious that if you don't know the values of row and col when you read this won't work. You will need to write them to the file as well and read the first when reading.

Sign up to request clarification or add additional context in comments.

Comments

1

Every pointer at ary is a different block at memory, so you need to write and read them separately:

Write:

for(int i=0;i<row;++i) 
   y.write((char*)&(M[i]),sizeof(int) * col);

Read:

for(int i=0;i<row;++i) 
   y.read((char*)&(M[i]),sizeof(int) * col);

If you post the definition of matrix I could help better.

1 Comment

Don't use C-style casts, use static_cast instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.