[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

Ravi

11/29/2008 7:41:00 PM

I was playing with object pointers here:
http://pastebin.com...

If I change line 46 to:

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

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

Kai-Uwe Bux

11/29/2008 7:55:00 PM

0

Ravi wrote:

> I was playing with object pointers here:
> http://pastebin.com...
>
> 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

julien.hamaide@gmail.com

11/29/2008 8:55:00 PM

0

On Nov 29, 8:40 pm, Ravi <ra.ravi....@gmail.com> wrote:
> I was playing with object pointers here:http://pastebin.com...
>
> 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 )++

Andrey Tarasevich

11/29/2008 8:57:00 PM

0

Ravi wrote:
> I was playing with object pointers here:
> http://pastebin.com...
>
> 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

juanvicfer

11/29/2008 11:44:00 PM

0

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

Thomas J. Gritzan

11/30/2008 1:07:00 AM

0

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

James Kanze

11/30/2008 10:05:00 AM

0

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

> > 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:james.kanze@gmail.com
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

James Kanze

11/30/2008 10:06:00 AM

0

On Nov 29, 9:54 pm, "julien.hama...@gmail.com"
<julien.hama...@gmail.com> wrote:
> On Nov 29, 8:40 pm, Ravi <ra.ravi....@gmail.com> wrote:

> > I was playing with object pointers here:http://pastebin.com...

> > 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:james.kanze@gmail.com
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

Ravi

12/1/2008 9:24:00 AM

0

> 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...
Now: http://pastebin.com...

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.

Triple-DES

12/1/2008 9:49:00 AM

0

On 1 Des, 10:24, Ravi <ra.ravi....@gmail.com> 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...
> Now:    http://pastebin.com...
>
> 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?

Ravi

12/2/2008 11:18:00 AM

0

> 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!!