c - Trying to write a function that takes an array of specific length, getting weird inconsistant results with no errors -


i have:

#include <stdio.h>  typedef float mat4f[16];  void logmat(mat4f in) {     int i;     (i=0; i<16; i++) {         //in[i] = i; //uncomment line , works expected         printf("%2d: %f\n", i, in[i]);     } }  int main(void) {      mat4f a;     logmat(a);      return 0; } 

when run this, elements @ index 4 , 12 appear corrupted. examples:

 0: 0.000000  1: 0.000000  2: 0.000000  3: 0.000000  4: -0.019316  5: 0.000000  6: 0.000000  7: 0.000000  8: 0.000000  9: 0.000000 10: 0.000000 11: 0.000000 12: -0.000000 13: 0.000000 14: 0.000000 15: 0.000000 

or

 0: 0.000000  1: 0.000000  2: 0.000000  3: 0.000000  4: 894113943650304.000000  5: 0.000000  6: 0.000000  7: 0.000000  8: 0.000000  9: 0.000000 10: 0.000000 11: 0.000000 12: 0.002546 13: 0.000000 14: 0.000000 15: 0.000000 

and different results running multiple times. however, if uncomment 1 line you'll see in source, works expected every time.

can spot mistake? why index 4 , 12?

i'm kinda trying suggested here: stackoverflow.com/a/1810295/1472246

you have found feature of c. source of tricky bugs though.

the problem line:

mat4f a; 

it reserves space variable a. not set value. a have value of data there previously. zero, time time else.

one way set a 0 declare initializer:

mat4f = {0}; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -