Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!wa4mei!holos0!lbr From: lbr@holos0.uucp (Len Reed) Newsgroups: comp.lang.c Subject: Re: time(0L) - history of a misconception (was Re: SCO password generator) Message-ID: <1991May28.175246.2160@holos0.uucp> Date: 28 May 91 17:52:46 GMT References: <1991May24.151350.22705@holos0.uucp> <1991May25.002706.27552@kithrup.COM> <1991May25.221530.16119@zoo.toronto.edu> Organization: Holos Software, Inc., Atlanta, GA Lines: 67 In article <1991May25.221530.16119@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes: >Actually, the people Len really wants to talk to are the IBM AS/400 people, >whose machine is an exception to almost every naive misconception about C >you have ever seen. Note: I've dropped the 386 group out of the distribution. Well, several folks have set my straight on NULL not not necessarily being all bits zero on some machines. It is true that (type *)0 must be the NULL pointer of type, even if done as part of an implicit cast. E.g., extern void some_routine(int *); .... some_routine(0); /* implicit cast here */ Obviously this is not well-coded, even if legal. I myself always use NULL instead of zero. Okay, what about static and global pointers not explicitly initialized? Older Unices (not sure about the newest) depended upon the kernel's zeroing out of the BSS to hit global and static data with zeros. (This is in some sense a side-effect of zeroing out data for security reasons: you can peek at what the last process did with the memory you get.) Are these zero or NULL? The compiler could catch these and make them NULL--does it? Example: static char *s_ptr; /* Is s_ptr NULL or all-zeros? */ What about pointers inside structs? E.g., struct my_struct { int *pointer; int abc; } instance; Is instance.pointer NULL or all zeros? Finally, what if I malloc a my_struct and memset it to zeros? No way this can work: if (a_ptr = malloc(sizeof(struct my_struct))) != NULL) { memset(a_ptr, 0, sizeof(struct my_struct)); } There's no way a_ptr->pointer is going to be NULL after this runs since the compiler can't get this one. I know that this'll break some of my code. I use this mechanism for various kinds of linked structures. If the static init works (instance.pointer above), I could define a constant template for initialization and use structure assignment instead of memset: struct my_struct initer = {NULL, 0}; or just struct my_struct initer; /* trust compiler to init this correctly */ .... *a_ptr = initer; /* instead of memset */ But I don't do it now, and I know lots of folks who don't. -- Len Reed Holos Software, Inc. Voice: (404) 496-1358 UUCP: ...!gatech!holos0!lbr