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.
M? Please provide MCVE.newing everything you create. We havestd::vector,std::array, and language-level arrays. You don't have tonewevery object you use.sizeof Min yourreadandwritecalls are compile-time value. Let's say thatsizeof Mis 50 bytes. That 50 doesn't change value, no matter what the dimensions of your matrix are. It could be1 x 1or10000 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.