-1

I'm quite new to C programming and am running in to a strange problem. I have a global variable tri which is a struct (tri_fact) with a couple of members. Two of these members (tri.Q and tri.R) are in turn of a different kind of struct (dense_mat) for dense matrices.

I have a function to initialize some of these dense matrices to zero. The problem I'm having is that one of these matrices has junk values when the initialization function is exited. Right before exiting, however, everything looks good.

struct tri_fact tri;
void init_func(){
    /*do some initializations*/
    /*tri.Q and tri.R looks good*/
}
void func(){
    init_func();
    /* tri.R looks good, but tri.Q is filled with junk */
}

Am I missing something fundamental?

Erik
  • 11
  • 1

1 Answers1

1

The fundamental notion you should be aware of is undefined behavior. See this to understand that it is serious. Read references from here.

Did you compile your entire program (all the code) with all warnings and debug info? With GCC -e.g. on Linux- that means gcc -Wall -Wextra -g.

Then you should use the debugger (gdb on Linux) and probably use watchpoints. Perhaps disabling ASLR could be worthwhile to find your bug.

With a recent GCC you could also use the various -fsanitize= options, in particular compile with -fsanitize=address. The valgrind tool could also be extremely helpful.

Community
  • 1
  • 1
Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509