Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Object pointer casted to int* , wont increment with ++

Reply
Thread Tools

Object pointer casted to int* , wont increment with ++

 
 
Ravi
Guest
Posts: n/a
 
      11-29-2008
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.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-29-2008
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.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
julien.hamaide@gmail.com
Guest
Posts: n/a
 
      11-29-2008
On Nov 29, 8:40*pm, Ravi <(E-Mail Removed)> 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.


Even if it's full of undefined behavior, (int*)px1++ execute first px1+
+ then (int*),

try to scope the cast ( (int*)px1 )++
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-29-2008
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.


Doesn't increment? Sorry, but that doesn't make any sense. Nowhere in
your code after line 46 I see anything that would demonstrate whether
the pointer got incremented or not. You don't output the new value of
'px1', you don't output anything that depends on the new value of 'px1'.
What made you conclude that it doesn't increment then?

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
juanvicfer
Guest
Posts: n/a
 
      11-29-2008
> > If I change line 46 to:
>
> > * * * * double *pd = (double *)((int *)px1++);

>
> > The pointer doesn't increment as shown by the output at stdout.

>
> Doesn't increment? Sorry, but that doesn't make any sense. Nowhere in
> your code after line 46 I see anything that would demonstrate whether
> the pointer got incremented or not. You don't output the new value of
> 'px1', you don't output anything that depends on the new value of 'px1'.
> What made you conclude that it doesn't increment then?


I suppose that he is expecting to have the address of px1 + size_of
(int);

This can be achieved with the following expression:

double *pd = (double *)(++(int *)px1);

, although the behaviour is undefined in this case


Regards
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      11-30-2008
juanvicfer wrote:
>>> If I change line 46 to:
>>> double *pd = (double *)((int *)px1++);
>>> The pointer doesn't increment as shown by the output at stdout.

>> Doesn't increment? Sorry, but that doesn't make any sense. Nowhere in
>> your code after line 46 I see anything that would demonstrate whether
>> the pointer got incremented or not. You don't output the new value of
>> 'px1', you don't output anything that depends on the new value of 'px1'.
>> What made you conclude that it doesn't increment then?

>
> I suppose that he is expecting to have the address of px1 + size_of
> (int);
>
> This can be achieved with the following expression:
>
> double *pd = (double *)(++(int *)px1);


This doesn't (or should not) compile. (int*)px1 yields an rvalue in this
case, but the increment operator only works on lvalues, because it
changes the operand.

> , although the behaviour is undefined in this case


--
Thomas
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-30-2008
On Nov 29, 8:55*pm, Kai-Uwe Bux <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-30-2008
On Nov 29, 9:54*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> On Nov 29, 8:40*pm, Ravi <(E-Mail Removed)> 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.


> Even if it's full of undefined behavior, (int*)px1++ execute
> first px1+ + then (int*),


> try to scope the cast ( (int*)px1 )++


That shouldn't compile.

The only reasonable advice one can give him is to forget it, and
avoid this site in the future.

--
James Kanze (GABI Software) email:(E-Mail Removed)
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
 
Reply With Quote
 
Ravi
Guest
Posts: n/a
 
      12-01-2008
> avoid this site in the future.

This is my own code, I used the site to post it because it makes it
easy for other to read the code (syntax highlighting).

Yes, this code is not to be used anywhere in real world and should not
be used for serious applications. But it fulfills the learning
objective that C++ classes are similar to C struct.

Most of you gave me the answers (apart from the positive criticism)
that I may have missed operator precedence. However to remove that
confusion I post another version of it.

Earlier: http://pastebin.com/f6bb9d1a0
Now: http://pastebin.com/f293d8b2c

Here I cast object pointer to int pointer in line 46 and then
increment the latter. I get the expected answer 1.121... in from line
50.
But if I change the line 47 to
double *pd = (double *)(t++);
I don't get the expected answer.
 
Reply With Quote
 
Triple-DES
Guest
Posts: n/a
 
      12-01-2008
On 1 Des, 10:24, Ravi <(E-Mail Removed)> wrote:
> This is my own code, I used the site to post it because it makes it
> easy for other to read the code (syntax highlighting).
>
> Yes, this code is not to be used anywhere in real world and should not
> be used for serious applications. But it fulfills the learning
> objective that C++ classes are similar to C struct.


To a degree, but as others have pointed out, your code contains so-
called undefined behaviour, which means that the behaviour and output
of the program could be basically anything, and will probably be
different on another compiler, or even the next time you run the
program.

My advice is to be careful about making assumptions about the C++
language based on the output of such a program.

> Most of you gave me the answers (apart from the positive criticism)
> that I may have missed operator precedence. However to remove that
> confusion I post another version of it.
>
> Earlier:http://pastebin.com/f6bb9d1a0
> Now: * *http://pastebin.com/f293d8b2c
>
> Here I cast object pointer to int pointer in line 46 and then
> increment the latter. I get the expected answer 1.121... in from line
> 50.
> But if I change the line 47 to
> * * double *pd = (double *)(t++);
> I don't get the expected answer.


I think you are confused about pre-increment / post-increment.
Consider the following analogous example:

int i = 1;
int j = (i++);
std::cout << j; // did you expect 2?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with a casted pointer Raj Pashwar C Programming 4 10-10-2011 06:29 PM
Problem on using a casted void pointer kkirtac C Programming 10 09-24-2007 09:41 AM
problem with a casted pointer praveen C Programming 15 07-22-2007 07:43 PM
Safely deleting a type-casted pointer Pat C++ 1 02-22-2007 07:33 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments