Hello all-
I am a fairly new C/C++ programmer and I am running into a very strange problem.
I have a simple nested for loop that is filling a location in memory using a pointer:
for (int j=0;j<n;j++)
for (int i=0;i<m;i++,ptr++)
*ptr=(float)i;
ptr simply points to a preallocted (m*n*sizeof(float)) location in memory.
This loop is clearly supposed to output a m*n length vector containing n-1 segments of increasing floats of 0:m-1. Since n*m=5332, I am expecting a vector of that size. I am using fwrite to write the vector to a binary file to view using MATLAB:
int chk = fwrite(matrix,sizeof(float),m*n,file);
where matrix is simply the location in memory that ptr originally pointed to.
However, when I view the data in MATLAB, I only get a vector of 5120. I have set breakpoints and debugged, and found that the chk int equals 5332 as expected. Also, I can look at matrix after the loop has executed when the debugger is stopped and I find values for matrix[5120 through 5332]. The problem is definitely not in the MATLAB code (I am a fairly experienced user), and I am convinced that the vector is actually not filling properly based on other results I see later in my algorithm.
So my question is, is there something that could cause the debugger to "see" the value of a variable in a memory location that is actually not there? This is also confusing because fwrite seems to indicate that it has written successfully values 5120-5332 to binary, even though I can not see them when I open the file.
Again, I know the error is not in the memory allocation or in the for loops since I can use Quickwatch and see the correct values for the entire vector.
I can provide more information if necessary; I have not fully explained my code to keep my question simple.
Thank you both for your replies. I am opening the file using "wb+" so I do not think that this it is an issue of how I am saving. I use the same method to save data in other parts of my program and everything looks fine there.
Based on other outputs later in my program, I know that this particular vector is not filling completely; however, when I debug, the debugger tells me that there should be data at the end of the matrix even though there is not. Is there any scenario in which this would arise? I am using the debugger in Visual Studio .NET 2003. Another consideration: I know that my program has a lot of memory leaks in it that I have yet to correct, could this affect my program in this way?
Ptr is not a function pointer, it is just a pointer that was initialized to the first element of the memory location "matrix", and is incremented in the loop.
So the byte size of the file is incorrect--that is, it is not 5332*4=21328 bytes, but rather 5120*4=20480 bytes exactly. And when I read the data with MATLAB, I see exactly 5120 elements.
So we seem to have arrived at a fundamental discrepancy; fwrite returns the value 5332, indicating the correct number of elements written, but the file size indicates only 5120 elements are written. Could anybody shed some light on a potential cause of this? This is very confusing to me.
Thank you all in advance.
P.S. There is some code between the nested for loops and the fwrite, but they are simply other for loops that are filling other locations in memory (all the locations were previously defined with malloc).
Your problem looks similar in that 20480 is a multiple of page size (4096) and so it looks like the write buffer
is not finishing before you read the file in.