Berkeley CSUA MOTD:Entry 27702
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2024/11/26 [General] UID:1000 Activity:popular
11/26   

2003/3/15 [Computer/SW/Languages/C_Cplusplus] UID:27702 Activity:very high
3/14    Are the fields stack-allocated structs initialized to zero? (C++)
        \_ Nope.  But, like C, you can always write MyStruct s = { };
           to declare a struct s and zero-initialize it.  --mconst
           \_ What is the difference between this and:
              memset(struct,'\0',sizeof(struct))
              I usually use memset, but I'd rather use something else
              that involves less typing if it accomplishes the same
              thing.
              \_ There's no difference at all, unless you're on a weird
                 machine that doesn't store zero values as all bits zero;
                 gcc compiles the two to the exact same code.  --mconst
                 \_ Thanks.
                 \_ it's not so weird for null pointers to be non-zero, is it?
                    uncommon, but not unheard of.
              \_ I believe memset will work with heap-alloc'ed memory
                 but "= { }" will not.  man bzero, it's a little bit
                 less typing.
                 \_ I'd use bzero, but it is deprecated on Linux and
                    other platforms.
                 \_ of course the "= { }" won't work on heap allocated memory,
                    because you can only initialize structures like that when
                    they're declared, and declared variables are stored on the
                    stack.
           \_ thanks, glad to know this now.