struct - Global variable change value when exiting function C -


i'm quite new c programming , running in strange problem. have global variable tri struct (tri_fact) couple of members. 2 of these members (tri.q , tri.r) in turn of different kind of struct (dense_mat) dense matrices.

i have function initialize of these dense matrices zero. problem i'm having 1 of these matrices has junk values when initialization function exited. right before exiting, however, looks good.

struct tri_fact tri; void init_func(){     /*do initializations*/     /*tri.q , tri.r looks good*/ } void func(){     init_func();     /* tri.r looks good, tri.q filled junk */ } 

am missing fundamental?

the fundamental notion should aware of undefined behavior. see this understand serious. read references here.

did compile entire program (all code) warnings , debug info? gcc -e.g. on linux- means gcc -wall -wextra -g.

then should use debugger (gdb on linux) , use watchpoints. perhaps disabling aslr worthwhile find bug.

with recent gcc use various -fsanitize= options, in particular compile -fsanitize=address. valgrind tool extremely helpful.


Comments