简体   繁体   中英

Static variable initialized with wrong value

I've defined a function like this

static void
flatten_tree(...)
{
  static int num = 0;
  ...
}

However, when the function is executed, num gets some random value, rather than 0 on the first run. Upon subsequent function calls it behaves as static var should do. Aren't static variables explicitly initialized with zero? What is the proper way to initialize it?

If it's not set to zero the first time it's called then either your C implementation is broken, your debugger is broken, or you've made some other error - I won't comment on which is the most likely scenario :-)

If you really want to see what it's doing, chage your code temporarily to something like:

static void
flatten_tree(...)
{
  static int num = 0;
  printf( "XYZZY DEBUG: initial num is %d\n", num);
  exit(1);

  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM