Aw: Well defined meaning of pointer to an array

Discussion in 'C++' started by Syron, Apr 18, 2011.

  1. Syron

    Syron Guest

    Just to put some oil into to fire... What is the following, Paul?

    struct Foo {
    int data[100];
    };
    int* bar = (int*)(new Foo);

    The fact is that bar points to simply data, nothing else. The language structs/type-system are helpers for interpreting data the right way. Data and its interpretation (here as an int) is absolutely independent, thus it can be considered a nice coincident here that the second declaration points to astorage area that has been declared as an int earlier.

    Arrays are only unaligned, continious storage areas of the same type, and the fact that an array alias (in the sense of "alias of a storage area") canbe implicitly converted into a pointer is just a convenience thing to iterate over the array elements.

    Thus, when you write something like:
    int A[100];
    int* B = A;
    B is both a pointer to an int as well as a pointer to an array of ints.

    But my personal opinion is to call them the following:
    - A is an alias for a storage area of the size of 100*sizeof(int).
    - B is an alias for a storage area that can hold an address of memory.
    - When dereferenced (by an array index specifier or by a pointer dereferencing asterisk), the memory referenced gets interpreted as an int.
    - The fact that the elements at B+n*sizeof(int) with 1<n<100 /also/ have been declared as ints is nice, but nothing else.

    By the way, 0x3f000000 interpreted as a float is 1.0.

    (No, I'm no expert, but I spent some time thinking about ... Paul. These are some conclusions about my thoughts, and maybe they're wrong.)
     
    Syron, Apr 18, 2011
    #1
    1. Advertising

  2. Syron

    Paul Guest

    Re: Well defined meaning of pointer to an array


    >"Syron" <> wrote in message
    >news:675375f5-7d4c-4ef5-99f5->...
    >Just to put some oil into to fire... What is the following, Paul?
    >
    >struct Foo {
    > int data[100];
    >};
    >int* bar = (int*)(new Foo);
    >
    >The fact is that bar points to simply data, nothing else. The language
    >structs/type-system are helpers for >interpreting data the right way. Data
    >and its interpretation (here as an int) is absolutely independent, thus it
    >can >be considered a nice coincident here that the second declaration
    >points to a storage area that has been >declared as an int earlier.

    This same concept can be represented by a void pointer.
    void* p= almost_anything;

    Remember a pointer doesn't alwas point to simple data , it may point to a
    function for example. I prefer to think of a pointer as pointing to an
    entity. This way its suitable for pointing to functions, arrays, polymorphic
    types and simple data types.

    >
    >Arrays are only unaligned, continious storage areas of the same type, and
    >the fact that an array alias (in the >sense of "alias of a storage area")
    >can be implicitly converted into a pointer is just a convenience thing to
    > >iterate over the array elements.

    It's not just a convenience, its the underlying techincality because
    historically an array is a pointer+offset in von neuman architecture. I
    don't know of any system that has any other technical implementation.
    >
    >Thus, when you write something like:
    > int A[100];
    > int* B = A;
    >B is both a pointer to an int as well as a pointer to an array of ints.


    Yes I totally agree.
    An array of ints is an entity that could be described as a contiguous
    sequence of ints..
    >
    >But my personal opinion is to call them the following:
    >- A is an alias for a storage area of the size of 100*sizeof(int).
    >- B is an alias for a storage area that can hold an address of memory.
    >- When dereferenced (by an array index specifier or by a pointer
    >dereferencing asterisk), the memory >referenced gets interpreted as an int.
    >- The fact that the elements at B+n*sizeof(int) with 1<n<100 /also/ have
    >been declared as ints is nice, but >nothing else.

    I think it should be 0<= n < 100.
    What do you mean by "it's nice but nothing else"?
    It's not just a random consequence that happens to be nice. It's the way
    it's guaranteed to be, by the C++ standards.

    >
    >By the way, 0x3f000000 interpreted as a float is 1.0.

    I don't think all implementations represent floating point numbers in the
    same way.
    I don't know where this random statement about floating point conversions
    fits in with the rest of your post. :)

    >
    >(No, I'm no expert, but I spent some time thinking about ... Paul. These
    >are some conclusions about my >thoughts, and maybe they're wrong.)


    Your thoughts are welcome, Its makes a change from the repetative barrage of
    nonsense I have to deal with form the numerous
    http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm type
    characters on this forum.
     
    Paul, Apr 18, 2011
    #2
    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. Oodini
    Replies:
    1
    Views:
    1,838
    Keith Thompson
    Sep 27, 2005
  2. Replies:
    4
    Views:
    816
  3. Paul
    Replies:
    65
    Views:
    1,409
  4. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    495
    James Kuyper
    Sep 23, 2011
  5. Francois Grieu

    Is pointer arithmetic within a struct well defined?

    Francois Grieu, Nov 29, 2013, in forum: C Programming
    Replies:
    23
    Views:
    310
    Seebs
    Dec 4, 2013
Loading...

Share This Page