Re: well-defined terminology versus generally accepted terminologyregarding pointers and arrays

Discussion in 'C++' started by SG, Apr 13, 2011.

  1. SG

    SG Guest

    On 13 Apr., 14:27, cg_chas wrote:
    > Needless to say, there is significant dissension regarding when, or even if, it
    > is ever preferrable to use well-defined terminology instead of loose or casual
    > but generally accepted terminology regarding pointers and arrays.
    >
    > Examples such as:
    >    int a[42];
    >    int* p2 = a;
    > apparently create huge debates about how to describe the above expressions.
    >
    > Similar examples include expressions like:
    > int* p1 = new int[42];
    >
    > [...]
    > In any C++ discussion about pointer to T, is what is claimed to be "generally
    > acceptable" (the statement that an int* points to an array) ever truly
    > technically correct?
    > No.


    That depends on your definition of "points to", does it not?
    Personally, I distinguish between two cases here:

    (a) "p IS a pointer to..." as in
    "the static type of p is pointer to..."

    (b) "p points to..." as in
    "p currently points to..."

    For me, what "p IS" depends on its declaration. Its declaration
    introduces p with a certain type and this type defines what it IS --
    regardless of the value it holds. This is analogous to "x IS an int"
    or "y IS a double".

    Also, I find it acceptable to say that a pointer p which has been
    declared like this

    int *p;

    "points to an array" after it has been initialized "to do so". I
    regard this phrasing as "loose but acceptable". But I would not say
    that "p IS a pointer to an array" because "BEING a pointer to an
    array" implies for me that the static type is a pointer to an array.
    So, p IS a pointer to an int object (which may or may not be an
    element of an array) depending on its runtime value.

    If we check the standard for occurences of "pointer to" we'll find the
    following things:

    "if a pointer p whose static type is 'pointer to class B' is
    pointing an object of class D, derived from B, the dynamic type
    of the expression *p is 'D.' [...]"

    So, p with a static type B* can point to an object of type D (derived
    from B).

    "argv[0] shall be a pointer to initial character of a..."

    Note the explicitness here w.r.t. initial character.

    "class X; // X is an incomplete type
    extern X* rp; // rp is a pointer to an incomplete type"

    This is an example of "is a" referring to the static type.

    I just searched clause 1-3 and did not find definitions that would
    unambiguously attach a precise meaning to "p is a poitner to..." nor
    "p points to...". So, I don't think, my interpretations from above are
    in conflict with the C++ ISO standard.

    Cheers!
    SG
    SG, Apr 13, 2011
    #1
    1. Advertising

  2. SG

    SG Guest

    On 13 Apr., 16:00, SG wrote:
    > On 13 Apr., 14:27, cg_chas wrote:
    >
    > > Needless to say, there is significant dissension regarding when, or even if, it
    > > is ever preferrable to use well-defined terminology instead of loose orcasual
    > > but generally accepted terminology regarding pointers and arrays.

    >
    > > Examples such as:
    > >    int a[42];
    > >    int* p2 = a;
    > > apparently create huge debates about how to describe the above expressions.

    >
    > > Similar examples include expressions like:
    > > int* p1 = new int[42];

    >
    > > [...]
    > > In any C++ discussion about pointer to T, is what is claimed to be "generally
    > > acceptable" (the statement that an int* points to an array) ever truly
    > > technically correct?
    > > No.

    >
    > That depends on your definition of "points to", does it not?
    > Personally, I distinguish between two cases here:
    >
    >   (a) "p IS a pointer to..." as in
    >       "the static type of p is pointer to..."
    >
    >   (b) "p points to..." as in
    >       "p currently points to..."
    >
    > For me, what "p IS" depends on its declaration. Its declaration
    > introduces p with a certain type and this type defines what it IS --
    > regardless of the value it holds. This is analogous to "x IS an int"
    > or "y IS a double".
    >
    > Also, I find it acceptable to say that a pointer p which has been
    > declared like this
    >
    >   int *p;
    >
    > "points to an array" after it has been initialized "to do so". I
    > regard this phrasing as "loose but acceptable". But I would not say
    > that "p IS a pointer to an array" because "BEING a pointer to an
    > array" implies for me that the static type is a pointer to an array.
    > So, p IS a pointer to an int object (which may or may not be an
    > element of an array) depending on its runtime value.
    >
    > If we check the standard for occurences of "pointer to" we'll find the
    > following things:
    >
    >   "if a pointer p whose static type is 'pointer to class B' is
    >    pointing an object of class D, derived from B, the dynamic type
    >    of the expression *p is 'D.' [...]"
    >
    > So, p with a static type B* can point to an object of type D (derived
    > from B).


    Adding to that: It's an example where "points to" has been used to
    refer to a dynamic (aka runtime) property of a pointer.

    >   "argv[0] shall be a pointer to initial character of a..."
    >
    > Note the explicitness here w.r.t. initial character.


    I consider this noteworthy. I did not find any instance where the C++
    standard would use the kind of phrasing I regard as "loose but
    acceptable".

    >   "class X;      // X is an incomplete type
    >    extern X* rp; // rp is a pointer to an incomplete type"
    >
    > This is an example of "is a" referring to the static type.


    Throughout the standard "is a..." seems to be only used as shortcut
    for "has the static type...".

    > I don't think, my interpretations from above are
    > in conflict with the C++ ISO standard.


    In fact, I would say, that it actually supports my distinction between
    "being a pointer to..." when it comes to the static type and "points
    to..." when it's about a dynamic (aka runtime) property of the
    pointer.

    Cheers!
    SG
    SG, Apr 13, 2011
    #2
    1. Advertising

  3. SG

    Noah Roberts Guest

    On 4/13/2011 7:26 AM, SG wrote:
    > On 13 Apr., 16:00, SG wrote:


    >> If we check the standard for occurences of "pointer to" we'll find the
    >> following things:
    >>
    >> "if a pointer p whose static type is 'pointer to class B' is
    >> pointing an object of class D, derived from B, the dynamic type
    >> of the expression *p is 'D.' [...]"
    >>
    >> So, p with a static type B* can point to an object of type D (derived
    >> from B).

    >
    > Adding to that: It's an example where "points to" has been used to
    > refer to a dynamic (aka runtime) property of a pointer.


    Yes, but not in the same way that int* has been said to "point to" an
    array. This clause, and clauses like it, are explicitly about
    polymorphic objects. Polymorphic objects behave differently than other
    objects.

    #include <typeinfo>
    #include <iostream>
    struct B0 {};
    struct B1 { virtual ~ B1() {}};

    struct D0 : B0 {};
    struct D1 : B1 {};

    int main()
    {
    D0 d0; B0 * b0p = &d0;
    D1 d1; B1 * b1p = &d1;

    std::cout << typeid(*b0p).name() << std::endl;
    std::cout << typeid(*b1p).name() << std::endl;
    }

    Compile that code and you'll see that b0p does not have any extra
    information while b1p does. Only polymorphic types have "dynamic"
    types. What we're really seeing here is not an exception to what
    pointers point at, but the fact that when you ask a polymorphic object
    for its type it will give you its "dynamic" type while a non-polymorphic
    one won't even respond, leaving the compiler to interpret the request by
    simply checking the type information of the pointer itself, which of
    course remains B and not D.

    As you'll note it actually says that, "the dynamic type of the
    expression is of type D." It does not say that the type of the
    expression is of type D. Dynamic lookup is a different, extra beast
    allowed for and by polymorphic types. You still can't get enough
    information from the pointer to call members only in the derived class,
    or functions that work directly with it. You still have to cast in
    order to do so.

    >> I don't think, my interpretations from above are
    >> in conflict with the C++ ISO standard.

    >
    > In fact, I would say, that it actually supports my distinction between
    > "being a pointer to..." when it comes to the static type and "points
    > to..." when it's about a dynamic (aka runtime) property of the
    > pointer.


    Not really. The distinction would seem to more accurately be that the
    pointer "points to..." a polymorphic type and polymorphic types allow
    extra RTTI look-up procedures.

    --
    http://crazycpp.wordpress.com
    Noah Roberts, Apr 13, 2011
    #3
  4. On Apr 13, 7:00 am, SG <> wrote:
    > Personally, I distinguish between two cases here:
    >
    >   (a) "p IS a pointer to..." as in
    >       "the static type of p is pointer to..."
    >
    >   (b) "p points to..." as in
    >       "p currently points to..."
    >
    > For me, what "p IS" depends on its declaration. Its declaration
    > introduces p with a certain type and this type defines what it IS --
    > regardless of the value it holds. This is analogous to "x IS an int"
    > or "y IS a double".


    I fully agree, and I would be highly dubious of anyone who spoke
    differently. I've stated as much several times elsewhere, but I just
    want to make this clear. "X is a pointer to <some type>" is a
    statement about the (static) type of the variable X. "X points to
    <some object>" is a statement about the runtime value of the variable
    X. Moreover, it's presumably a description of the runtime value under
    the interpretation of the static type of X.

    Discussions of static types are clearly defined by the standard. IMHO,
    discussions of "points to" is less clearly defined, and frankly I
    think that pedantic debates on this are not useful, which is why I
    haven't really participated.

    To sum it up:
    int* x = new int[10];

    Is x a pointer to int? Yes. Is x a pointer to an array of int? No.
    This is not open to reasonable debate. Its (static) type is clearly
    "int*", and its (static) type is clearly not "int (*)[N]".

    Does x point to an array of int? Maybe. AFAIK, under normal
    conversation, it would not be confusing to say that, nor would it
    normally be considered incorrect or uncouth to say that. I have said
    that, and I think I'll continue to say that in my everyday programming
    discussions, including in my future postings here. Pedantically, is
    that wrong? Again, I'm not sure that I care.

    Either way Paul still has no clue what he's talking about, and should
    be ignored for the troll or troll-in-effect that he is.
    Joshua Maurice, Apr 14, 2011
    #4
  5. Am 14.04.2011 05:57, schrieb Joshua Maurice:
    > On Apr 13, 7:00 am, SG<> wrote:
    >> Personally, I distinguish between two cases here:
    >>
    >> (a) "p IS a pointer to..." as in
    >> "the static type of p is pointer to..."
    >>
    >> (b) "p points to..." as in
    >> "p currently points to..."
    >>
    >> For me, what "p IS" depends on its declaration. Its declaration
    >> introduces p with a certain type and this type defines what it IS --
    >> regardless of the value it holds. This is analogous to "x IS an int"
    >> or "y IS a double".

    >
    > I fully agree, and I would be highly dubious of anyone who spoke
    > differently. I've stated as much several times elsewhere, but I just
    > want to make this clear. "X is a pointer to<some type>" is a
    > statement about the (static) type of the variable X. "X points to
    > <some object>" is a statement about the runtime value of the variable
    > X. Moreover, it's presumably a description of the runtime value under
    > the interpretation of the static type of X.
    >
    > Discussions of static types are clearly defined by the standard. IMHO,
    > discussions of "points to" is less clearly defined, and frankly I
    > think that pedantic debates on this are not useful, which is why I
    > haven't really participated.
    >
    > To sum it up:
    > int* x = new int[10];
    >
    > Is x a pointer to int? Yes. Is x a pointer to an array of int? No.
    > This is not open to reasonable debate. Its (static) type is clearly
    > "int*", and its (static) type is clearly not "int (*)[N]".
    >
    > Does x point to an array of int? Maybe. AFAIK, under normal
    > conversation, it would not be confusing to say that, nor would it
    > normally be considered incorrect or uncouth to say that. I have said
    > that, and I think I'll continue to say that in my everyday programming
    > discussions, including in my future postings here. Pedantically, is
    > that wrong? Again, I'm not sure that I care.
    >
    > Either way Paul still has no clue what he's talking about, and should
    > be ignored for the troll or troll-in-effect that he is.


    Well said (the whole post). Whole-heartedly agree. Thumbs up, and all
    that...


    Peter
    Peter Remmers, Apr 14, 2011
    #5
    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. Alf P. Steinbach /Usenet
    Replies:
    61
    Views:
    1,026
  2. Ian Collins
    Replies:
    9
    Views:
    269
    Alf P. Steinbach /Usenet
    Apr 14, 2011
  3. Paul
    Replies:
    63
    Views:
    1,243
  4. Paul Butcher
    Replies:
    12
    Views:
    702
    Gary Wright
    Nov 28, 2007
  5. Thufir
    Replies:
    8
    Views:
    170
    Robert Dober
    Oct 24, 2007
Loading...

Share This Page