Re: Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"]

Discussion in 'C++' started by Robert Macy, Oct 26, 2005.

  1. Robert Macy

    Robert Macy Guest

    Alf,

    I don't think I can find errors for you, but read it I will.

    Small note, the "green" color for highlighting on my screen is almost
    no highlight whatsoever. Looks like the adjacent letters, unless I
    really, really look at it.

    Good subject, pointers.

    - Robert -
     
    Robert Macy, Oct 26, 2005
    #1
    1. Advertising

  2. Robert Macy

    Robert Macy Guest

    Alf,

    The program called bad_pointer02.cpp on page 4 [page 6 of pdf] returns
    the proper value, 43. It did not seem to yield undefined behaviour.

    I use MSVC line command running on XP.

    I can't see where the program is wrong. The function is called, x is
    created, the pointer is set to the location, and then return...oops. I
    get it. But the program still returned the proper value. hmm... Bet
    this happens everytime until you count on it.

    - Robert -
     
    Robert Macy, Oct 26, 2005
    #2
    1. Advertising

  3. Robert Macy

    Mirek Fidler Guest

    Re: Pointers and polymorphism explained [preview, PDF, part of myattempted "Correct C++ Tutorial"]

    Robert Macy wrote:
    > Alf,
    >
    > The program called bad_pointer02.cpp on page 4 [page 6 of pdf] returns
    > the proper value, 43. It did not seem to yield undefined behaviour.


    Actully, that is the tricky part of undefined behaviour. It often works
    on one platform and fails on another.

    Mirek
     
    Mirek Fidler, Oct 26, 2005
    #3
  4. * Robert Macy:
    >
    > Small note, the "green" color for highlighting on my screen is almost
    > no highlight whatsoever. Looks like the adjacent letters, unless I
    > really, really look at it.


    Yes, that was intentionally. The idea is to make it clear, as one reads
    the text, that this word or term is a new one, defined by the context.
    Without drawing the eyes towards that word or term, and without making
    it seem like a good idea to just skim those words or terms.

    Originally they weren't even boldface.

    But then I changed my mind: without boldface they appeared with _less_
    contrast than the rest of the text...

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 27, 2005
    #4
  5. * Robert Macy:
    > Alf,
    >
    > The program called bad_pointer02.cpp on page 4 [page 6 of pdf] returns
    > the proper value, 43. It did not seem to yield undefined behaviour.
    >
    > I use MSVC line command running on XP.
    >
    > I can't see where the program is wrong. The function is called, x is
    > created, the pointer is set to the location, and then return...oops. I
    > get it. But the program still returned the proper value. hmm... Bet
    > this happens everytime until you count on it.


    Thanks for that comment, it's appreciated.

    It just so happens that with the compiler options etc. you're using,
    there's nothing that changes the contents of the chunk of memory that
    the local variable occupied, between the time the pointer is obtained,
    and the time the pointer is used -- Undefined Behavior is allowed to
    do anything, including what someone might expect it to do, or not.

    One simple way to trash that memory (overwrite it with something else),
    is to call some function or, even just evaluating a suitable expression.

    E.g., I think something like

    std::cout << "Oops!" << std::endl;

    after the call to wrongAnswer(), but before displaying the contents of
    *p, would do the trick.

    I'll probably put in a statement like the "Oops!", to make it even more
    clear that this program is really really Undefined Behavior.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 27, 2005
    #5
  6. Robert Macy

    Robert Macy Guest

    Alf P. Steinbach wrote:
    > E.g., I think something like
    >
    > std::cout << "Oops!" << std::endl;
    >
    > after the call to wrongAnswer(), but before displaying the contents of
    > *p, would do the trick.


    Didn't mess up either. So I was going to try some other things, where
    I needed another pointer, like...
    int* p, q;
    p = wrongAnswer(); // this line generates a compile error
    q = p;
    std::cout << *q << std::endl;

    compiler says : error C2440: cannot convert from 'int' to 'int *'
    Conversion from integral type to pointer type requires
    reinterpret_cast, C-style cast or function-style cast

    didn't like this line either
    *p = wrongAnswer();

    as expected, this works...
    int* q;
    int* p = wrongAnswer();
    but the first failure makes no sense to me.

    ARRRRGGGG!!!!!

    instead of
    int* p, q; //which looks like it should work to me

    must do it this way...
    int* p;
    int* q;

    THEN THE PROGRAM COMPILES. But with a warning !

    Always gave a warning, just didn't notice until now.
    warning C4172: returning address of local variable or temporary

    Why is it not possible to use one line to declare pointers?
    int* p, q; // just not allowed?

    - Robert -
     
    Robert Macy, Oct 27, 2005
    #6
  7. * Robert Macy:
    > Alf P. Steinbach wrote:
    > > E.g., I think something like
    > >
    > > std::cout << "Oops!" << std::endl;
    > >
    > > after the call to wrongAnswer(), but before displaying the contents of
    > > *p, would do the trick.

    >
    > Didn't mess up either.


    It did when I tried it.


    [snip]
    > Why is it not possible to use one line to declare pointers?
    > int* p, q; // just not allowed?


    There is an example & discussion at the start of section 1.1.1 (top of
    page 2).

    You can use a typedef of the pointer type.

    But generally it's not a good idea to do that for the purpose of
    declaring more than one pointer at once: instead, declare each pointer
    in its own declaration.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 27, 2005
    #7
  8. Robert Macy wrote:
    > Why is it not possible to use one line to declare pointers?
    > int* p, q; // just not allowed?


    It IS possible. You just need to realize that the * belongs to the
    variable, not the type. Hence,
    int *p, *q;

    Which (mostly) is a good thing, as it allows you to declare type variables
    and type pointer variables in the same statement. E.g.
    int i, j, *p, *q;
    declares two ints and two int pointers.


    --

    Sigurd
    http://utvikling.com
     
    Sigurd Stenersen, Oct 27, 2005
    #8
  9. Robert Macy

    BobR Guest

    Robert Macy wrote in message
    <>...
    >Alf P. Steinbach wrote:
    >> E.g., I think something like
    >>
    >> std::cout << "Oops!" << std::endl;
    >>
    >> after the call to wrongAnswer(), but before displaying the contents of
    >> *p, would do the trick.

    >
    >Didn't mess up either. So I was going to try some other things, where
    >I needed another pointer, like...
    > int* p, q;
    > p = wrongAnswer(); // this line generates a compile error
    > q = p;


    [ Let's temporarily assume 'wrongAnswer()' did return a valid
    pointer-to-int.]
    Ouch!! You got bit by the ol' declaration-on-the-same-line trick.

    q = *p; // q is an 'int', NOT a pointer.

    That's why I like to 'stick the star onto the var'.
    int *p, *q;
    And you'll often see experts scream, "one declaration per line.".
    int *p;
    int *q;

    >Why is it not possible to use one line to declare pointers?
    > int* p, q; // just not allowed?
    >
    > - Robert -


    It is, BUT, the pointer does not bind to the 'int', but to the var ('p' in
    this example). Mr. Eckel, in TiCpp v1 Chap 3, states it like:
    (you can say that “* binds more tightly to the identifier”).

    That help? (I'm trying *not* to step on Mr. Steinbach's star(pun intended) ,
    it's his show. (...and I consider him the 'mentor' and me 'D dummy' <G>) ).
    --
    Bob R
    POVrookie
     
    BobR, Oct 27, 2005
    #9
  10. On Thu, 27 Oct 2005 21:30:01 +0200, "Sigurd Stenersen"
    <> wrote:

    > int *p, *q;
    >
    >Which (mostly) is a good thing, as it allows you to declare type variables
    >and type pointer variables in the same statement. E.g.
    > int i, j, *p, *q;
    >declares two ints and two int pointers.


    You are correct that it is allowed. But it can lead to errors either
    for the unwary or the overworked and tired. Note that Robert Macy,
    who started this thread, made this type of error. I believe that it
    is a style issue. I would recommend, in general, having one
    declaration per line.
    --

    Best wishes,

    Bob
     
    Robert W Hand, Oct 28, 2005
    #10
  11. Robert Macy

    Robert Macy Guest

    I have actually adopted this technique throughout, including simple
    declarations.

    It allows me to "lump" the variables into sections that are easy for me
    to review, change, etc.

    Alas, the sequential/nonrealtime aspects prevent me from trusting using
    all the module-ifications possible in C++ so I'm stuck with some *very*
    lengthy inline coding. I wonder who started the thinking that a
    computer has all day to go do each/all task(s) you ask of it. Probably
    the same people that think a 100MB program is small.

    - Robert -
     
    Robert Macy, Oct 28, 2005
    #11
  12. Robert Macy

    Greg Comeau Guest

    In article <>,
    Robert Macy <> wrote:
    >Why is it not possible to use one line to declare pointers?
    > int* p, q; // just not allowed?


    It is possible, just not the way you think.
    Just like say a typedef is not just text substitution,
    the parsing of the above does not necessarily link the * and int,
    therefore in short p is an int *, and q is an int. if you
    want them both to be int * you would write:

    int *p, *q;

    due to how C++ and C treat declarators (which involve *'s
    among other things). However, usually you want this:

    int *p;
    int *q;

    and furthermore, the opportunity to intiialize them
    whenever and whereever possible, and to document them.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Nov 5, 2005
    #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.

Share This Page