Lost Data

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 all very much for your time.

Didn't finish reading everything, but there's a slight syntax error.
 
for (int i=0;i<m;i++;ptr++)

Replace the semicolon with a comma, otherwise ptr will never be incremented.

EDIT: If you can post your entire program, that would be helpful because I don't have the time to check all of your math :/
Last edited on
Ascii-
Thank you for the quick reply. Unfortunately, that syntax error was just a typo; it is correct in my actual code.

I really cannot think of a better way to describe my problem other than what is in my initial post. Rest assured though, there is no math at all that needs to be checked.

My question is more of a conceptual question, and one that is likely very easily answered by experienced C programmers. I purposely did not post my code because I figured these conceptual questions were likely preferred to posts that are simply lines of code.

Please let me know if there is something else needed to make my problem more clear.
Well, I'm more of a C programmer than a C++ one anyhow :) What debugger are you using?

Also, if you want the help of far more experienced programmers than me, I would move this post to the General C++ Programming forum.
Last edited on
Topic archived. No new replies allowed.