[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: NULL pointers.

From: Paul Smith <pausmith_at_nortelnetworks.com>
Date: 2003-10-14 00:33:42 CEST

%% Greg Hudson <ghudson@MIT.EDU> writes:

  gh> I brought this up in October 2000 and lost then; don't expect to win any
  gh> time soon. :)

  gh> http://subversion.tigris.org/servlets/ReadMsg?list=dev&msgId=152209

If you're really into solving this sort of problem, there's a trick I've
been using for a while that I believe the standard requires to work.

It turns out the C standard requires that a variable with static storage
duration that is not explicitly initialized will be initialized by the
compiler such that all integral values are set to 0 and all pointer
values are set to the null pointer constant.

It further turns out the standard says that for any initializer which
does not provide enough elements to initialize the entire aggregate, the
remaining elements shall be initialized using the same rules as if they
had static storage duration.

Sooooo, a simple way of being sure that your memory is initialized
properly, even on systems where a null pointer constant is not
all-zero-bits, is this:

  struct some_complex_struct *foo

  {
    struct some_complex_struct __tmp = { 0 };
    foo = malloc(sizeof(struct some_complex_struct));
    if (foo)
      mempcy(&foo, &__tmp, sizeof(struct some_complex_struct));
  }

    ...

Well, anyway, it's simpler than trying to write out assignments for all
the pointer parts of a structure :).

If you're a preprocessor kind of hacker, it's even possible to write the
above as a preprocessor macro that hides all the gross bits:

  #define SCALLOC(_t,_v) do{ _t __tmp = {0}; \
                             (_v) = malloc(sizeof(_t)); \
                             if (_v) \
                               mempcy(&(_v), &__tmp, sizeof(_t)); \
                         }while(0)

    ...

  struct some_complex_struct *foo;

  SCALLOC(struct some_complex_struct, foo);

It's not an expression, and it has macro side-effects, but... I believe
it's conforming. You might get slightly better performance, if you're
worried about it, by using a global variable rather than an automatic
variable. Of course that's a bit harder to work with.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@nortelnetworks.com>   HASMAT--HA Software Mthds & Tools
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Tue Oct 14 00:34:30 2003

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.