Assertion fails after using memcpy()
I have the following code, where the assertion fails. Can anyone explain me why?
double *E = (double *) malloc(sizeof(double) * voxelSpaceSize);
double *E_new = (double *) malloc(sizeof(double) * voxelSpaceSize);
// ...some manipulations inside E and E_new, the memory loc开发者_Python百科ations do not change though
...
memcpy(E, E_new, sizeof(double) * voxelSpaceSize);
for (int i=0; i<voxelSpaceSize; i++) {
assert(E[i] == E_new[i]);
}
By definition, the special floating-point value NaN
is not equal to itself: NaN == NaN
returns false. So now I'm betting the value at the unequal index is NaN
. You may want to print out the value at the index where the value's not equal to itself, rather than using assert
.
Assuming you want to copy from E
to E_new
, your arguments to memcpy
are in the wrong order -- the second argument is the source pointer, the first is the destination.
精彩评论