Thread: Is this not undefined behavior?

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266

    Is this not undefined behavior?

    I was looking through the winpcap documentation and noticed this bit of code...

    Code:
    /* From tcptraceroute, convert a numeric IP address to a string */
    #define IPTOSBUFFERS    12
    char *iptos(u_long in)
    {
        static char output[IPTOSBUFFERS][3*4+3+1];
        static short which;
        u_char *p;
    
        p = (u_char *)∈
        which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
        _snprintf_s(output[which], sizeof(output[which]), sizeof(output[which]),"%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
        return output[which];
    }
    local variable "which" is not initialized. Unless I'm missing something that's undefined behavior. Or is there some sort of trickery going on here that I'm not aware of? There's an evaluation of it yet that variable could contain anything during that first go around (or any after for that matter).

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,993
    I am thinking they are assuming it is zero; but, I have no idea if the rules support this assumption.
    I know that most compilers will set static values like this to zero.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,587
    local variable "which" is not initialized. Unless I'm missing something that's undefined behavior.
    Yes you're missing something. Both static and global variables are initialized to zero by default.

    Jim

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Well I know one thing, if it goes uninitialized, it will get the values left on the stack. Which is usually something abnormal like -344643.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,587
    But static variables don't go uninitialized, they wil be initialized to zero.

    Jim

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Also static variable aren't on the stack, instead they are in the data portion of a program. There's only one instance of each static variable, from the start of a program until the program ends.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rcgldr View Post
    Also static variable aren't on the stack, instead they are in the data portion of a program. There's only one instance of each static variable, from the start of a program until the program ends.
    In other words, this function is not threadsafe!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General question about undefined behavior
    By kjwilliams in forum C Programming
    Replies: 46
    Last Post: 06-18-2013, 01:51 PM
  2. Undefined behavior
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 02-18-2013, 11:14 PM
  3. Static vs. Dynamic Arrays, Getting Undefined Behavior
    By StefPrez in forum C++ Programming
    Replies: 11
    Last Post: 01-28-2012, 11:39 PM
  4. Is x=x++; Undefined Behavior?
    By envec83 in forum C Programming
    Replies: 5
    Last Post: 10-04-2011, 01:27 AM
  5. Undefined behavior from VC6 to 2k5
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-22-2011, 07:56 PM