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

K

Kai-Uwe Bux

Ravi said:
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
 
J

julien.hamaide

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 )++
 
A

Andrey Tarasevich

Ravi said:
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?
 
J

juanvicfer

If I change line 46 to:
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
 
T

Thomas J. Gritzan

juanvicfer said:
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.
 
J

James Kanze

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).
 
J

James Kanze

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.
 
R

Ravi

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.
 
T

Triple-DES

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?
 
R

Ravi

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?

Thanks a lot!!
 
R

Ravi

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

Advice well taken and I will keep a note of that while learning C++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top