On Nov 29, 8:55*pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Ravi wrote:
> > I was playing with object pointers here:
> >http://pastebin.com/f6bb9d1a0
> > If I change line 46 to:
> > * * * * double *pd = (double *)((int *)px1++);
> > The pointer doesn't increment as shown by the output at
> > stdout.
> You have undefined behavior all over the place: dereferencing
> or incrementing a pointer casted to a type that is not the
> type of the underlying object is undefined behavior.
His code is just a slight modification of the code he cited.
Which looks like some of the worst code I've seen in a long
time; he should really avoid that site, if that's the sort of
junk they post.
The original posting basically had:
struct X { int i; double d ; } ;
and px1 was a pointer to an X. It then did (double*)((int*)px1
+ 1), apparently expecting to access the d element. Which not
only is undefined behavior, but doesn't work with most of the
compilers I have access to (and will, in fact, generate a bus
error). His modification uses an operator with higher
precedence that the cast, so his incrementation results in
accessing an inexistant X; I get random values (which could
result in a floating point exception, if the random value
corresponded to a trapping NaN).
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34