In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>"Nick Keighley" <ni************ ******@hotmail. comwrites:
>besides the undefined behaviour of assigning an int to an int*
you also forgot to initialise *p to 12. There's no *way* the
printf() could print 12
>(ok, it's 1 chance in sizeof(int))
I think you mean something like one chance in UINT_MAX+1.
Not quite that, either.
There is a nonzero chance that the program won't be allowed to access
the memory that p is pointing at.
There's also a good chance that the bit pattern it's pointing at won't
have a uniform probability distribution; for example, many OSs zero-fill
memory before giving it to a program, and if the memory has been used
already in the program's run (f'rexample, by loading shared libraries
or initializing the runtime library) then the values left behind by that
use will probably follow fairly predictable patterns.
Given that the pointer can be dereferenced without causing a crash
and that the bit pattern follows a uniform probability distribution,
the probability that the value it points at will be 12 is (number of
representations of the value 12 for type int) chances in (two the power
of CHAR_BIT*sizeof (int)).
If there are no padding bits in int (so exactly one possible bit
pattern represents the value 12) or in unsigned int (so UINT_MAX+1 is
the number of possible bit patterns), then given accessibility of memory
and uniformly distributed patterns the likelihood of getting a 12 does
reduce to one chance in UINT_MAX+1, but it's not hard to construct a
case where it won't.
dave
--
Dave Vandervies
dj******@csclub .uwaterloo.ca
Two glaring problems and one subtle error is a better-than-average
outcome in these parts.
--Eric Sosman in comp.lang.c