Representation of NULL and 0.0 (was Re: Zero Length Arrays ...)

Martin Weitzel martin at mwtech.UUCP
Sat Dec 16 02:05:52 AEST 1989


In article <1989Dec14.182113.5398 at twwells.com> bill at twwells.com (T. William Wells) writes:
[most stuff deleted, because not significant for my question]
>
>Warning: null pointers and floating point zeros are *not*
>necessarily represented by bit patterns of all zero bits.

Sorry, my copy of the draft is not the most recent (we Europeans
seem to have no easy way, to get an up-to-date *A*NSI-Standard :-/),
but I allready guessed that a portable programm would require:

	foo(size_t n)
	{
		double *a;	/* no a[n] in C */
		a = malloc(n * sizeof *a);
		if (!a) abort();/* at least */
		{ size_t i = n; while (i--) a[i] = 0; }
		/* now we have an array of n zero-initialized double-s */
		/* and can do whatever we must do with it .... */
		:
		:
		free(a);
	}

If the type of "a" would have been int or long, one could ommit
the initialization loop and replace malloc with calloc, which may
have a faster way to zero the allocated space. (Am I right so far?)
Wouldn't it have been wise to add another 'calloc' for floating
types and NULL pointers, as the initializing feature of calloc
is weak in this respect? (As an aside, the pure existance of
such a function would have warned all programmers, who are still
in believe, they allways receive NULL-pointers or 0.0 from calloc).

Now, how is the situation with statically allocated data (esp.
not fully initialized arrays). The compiler 'knows' the exact
data type in this situation (other than calloc in the above example)
and could use 'the right' representation. What are the guarantees
of the standard: zero bits or NULL resp. 0.0?

-- 
<<< MW -- email: see header -- voice: 49-(0)6151-6 56 83 >>>



More information about the Comp.lang.c mailing list