Re: Is this OK?



g_new0() is a macro to allocate n structures (the 1 in this case) of a
type and zero it out.  You can free the structure just as you said.
Your code would work but is a little less concise (ie setting a pointer
to NULL and immediately assinging to it with g_malloc()). 

But this is broken.
The ISO C doesn't say the binary representation of the NULL pointer must
be 0, in the same way than the binary representation of 0.0 can be very
different from the binary representation of 0.
When you use g_new0() you the memory with bits at 0, which is the 
representation of the integer 0, not of the pointer NULL. When you write
(void *) 0, the C compiler convert the 0 into the binary representation of
NULL.

Yes, on common platforms, NULL is binary 0. But it can be different, and 
such code can break, since it relies upon something which is not defined
in the ISO C.

(If there are any reasons to *not* do it this way, please point them out
to me :)

Here it is :)

-- 
Gael Le Mignot "Kilobug" - kilobug freesurf fr - http://kilobug.free.fr
GSM         : 06.71.47.18.22 (in France)   ICQ UIN   : 7299959
Fingerprint : 1F2C 9804 7505 79DF 95E6 7323 B66B F67B 7103 C5DA

Member of HurdFr: http://hurdfr.org - The GNU Hurd: http://hurd.gnu.org



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]