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

Discussion in 'C++' started by Ravi, Nov 29, 2008.

  1. Ravi

    Ravi Guest

    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.
     
    Ravi, Nov 29, 2008
    #1
    1. Advertising

  2. Ravi

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Nov 29, 2008
    #2
    1. Advertising

  3. Ravi

    Guest

    On Nov 29, 8:40 pm, 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.


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

    try to scope the cast ( (int*)px1 )++
     
    , Nov 29, 2008
    #3
  4. 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
     
    Andrey Tarasevich, Nov 29, 2008
    #4
  5. Ravi

    juanvicfer Guest

    > > 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
     
    juanvicfer, Nov 29, 2008
    #5
  6. 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
     
    Thomas J. Gritzan, Nov 30, 2008
    #6
  7. Ravi

    James Kanze Guest

    On Nov 29, 8:55 pm, Kai-Uwe Bux <> 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
     
    James Kanze, Nov 30, 2008
    #7
  8. Ravi

    James Kanze Guest

    On Nov 29, 9:54 pm, ""
    <> wrote:
    > On Nov 29, 8:40 pm, 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.


    > 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:
    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, Nov 30, 2008
    #8
  9. Ravi

    Ravi Guest

    > 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.
     
    Ravi, Dec 1, 2008
    #9
  10. Ravi

    Triple-DES Guest

    On 1 Des, 10:24, Ravi <> 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?
     
    Triple-DES, Dec 1, 2008
    #10
  11. Ravi

    Ravi Guest

    > 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!!
     
    Ravi, Dec 2, 2008
    #11
  12. Ravi

    Ravi Guest

    > 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++
     
    Ravi, Dec 2, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pat
    Replies:
    1
    Views:
    274
    Victor Bazarov
    Feb 22, 2007
  2. praveen

    problem with a casted pointer

    praveen, Jun 29, 2007, in forum: C Programming
    Replies:
    15
    Views:
    489
    David Thompson
    Jul 22, 2007
  3. kkirtac

    Problem on using a casted void pointer

    kkirtac, Sep 23, 2007, in forum: C Programming
    Replies:
    10
    Views:
    654
    kkirtac
    Sep 24, 2007
  4. Raj Pashwar

    problem with a casted pointer

    Raj Pashwar, Oct 8, 2011, in forum: C Programming
    Replies:
    4
    Views:
    342
    Morris Keesan
    Oct 10, 2011
  5. Replies:
    2
    Views:
    95
    Barry Schwarz
    Feb 14, 2014
Loading...

Share This Page