c++ - Different results when I change the dimension of the array but for the same equation? -
i using ubuntu 14.04 , compiling c++ code gcc
. when m
, n
values (which can see in code below) equal 6
, printed x
values between 1
, 6
; , that's normal , correct; y
values start 7.0
, end 12.0
. y
values should start 1.0
, when m
, n
equal 5
, not having same issue. both x
, y
values start 1
end 6
. can help me that?
#include <iostream> #include <cstdlib> #include <cstdio> #define m 6 #define n 6 using namespace std; int main() { double f[8][n][m], feq, rho[n][m]; double x[n], y[m], w[8], dx, dy; dx = 1.0; dy = 1.0; x[0] = 0.0; y[0] = 0.0; int i,j; (i = 1; < n+1; i++) { x[i] = x[i-1] + dx; printf("%f\n",x[i]); } (j = 1; j < m+1; j++) { y[j] = y[j-1] + dy; printf("%f\n",y[j]); } homecoming 0; }
i'm pretty sure meant utilize for(i=1; i<n; i++){
, for(j=1; j<m; j++){
.
double x[6];
has 6 elements 0,1,2,3,4 , 5.
this means if write n[6] = 6
encountering undefined behavior. in case n
, m
happen next each other in memory , when write n[6]
write m[0]
. although happen c++ compilers can not rely on behavior. since it's undefined c++ standard compliant compiler not need create guarantee should happen in case.
as why doesn't happen n
, m
beingness 5. well, since undefined behavior there doesn't need consistency in behavior. related memory arrangement, compiler order x
, y
differently in memory or add together padding in-between.
c++ gcc
No comments:
Post a Comment