In (int a[3]; int *p;) is a and p both GENUINE pointers to integers?

Discussion in 'C++' started by Xiaozhong, Jan 22, 2008.

  1. Xiaozhong

    Xiaozhong Guest

    I recently find int *p is a GENUINE pointer, which means, p itself is
    an integer that takes 4 bytes in memory; however int a[3], where the
    array name a is a VIRTUAL pointer, whose physical address is just
    itself and could not be retrieved.

    I find that int *p creates a *genuine* pointer to an integer, where p
    is per se an 4-byte integer stored in memory. The value of p indicates
    the address of the integer it points to. Therefore, *p, p, &p should
    all be different. So far so good.

    However, as is said in many textbook, int a[4] create an array, with
    the array name 'a' as a pointer to this array.
    Therefore I just speculated that *a,a, &a should also be different
    values; I further guessed that creating and initializing a[4] consumes
    4*4bytes to hold the value of a[4] and another 4 byte pointer a to
    addressing the starting point of the array. But it is not the case in
    Visual C++ 2005. &a=a in the result. a itself does not have an
    address.

    Who can explain this? All comments are welcomed.

    #include <iostream>
    using namespace std;
    void main(){
    int a[]={1,2,3,4};
    for (int i=0;i<4;i++){
    printf("a[%d]:%4d|addr:%6x\n",i,a,&(a));
    }
    printf("\n\n");
    printf("&a: \t%8x\n",&a);
    printf("a: \t%8x\n",a);
    printf("*a:\t%8x\n",*a);


    int *p;
    p=new int(0xff);
    printf("&p:\t%8x\n",&p);
    printf("p:\t%8x\n",p);
    printf("*p:\t%8x\n",*p);
    }

    a[0]: 1|addr:12ff64
    a[1]: 2|addr:12ff68
    a[2]: 3|addr:12ff6c
    a[3]: 4|addr:12ff70




    &a: 12ff64
    a: 12ff64
    *a: 1

    &p: 12ff60
    p: 475fd0
    *p: ff
    Xiaozhong, Jan 22, 2008
    #1
    1. Advertising

  2. Xiaozhong

    jalina Guest

    Xiaozhong a écrit :
    > I recently find int *p is a GENUINE pointer, which means, p itself is
    > an integer that takes 4 bytes in memory; however int a[3], where the
    > array name a is a VIRTUAL pointer, whose physical address is just
    > itself and could not be retrieved.
    >
    > I find that int *p creates a *genuine* pointer to an integer, where p
    > is per se an 4-byte integer stored in memory. The value of p indicates
    > the address of the integer it points to. Therefore, *p, p, &p should
    > all be different. So far so good.
    >
    > However, as is said in many textbook, int a[4] create an array, with
    > the array name 'a' as a pointer to this array.
    > Therefore I just speculated that *a,a, &a should also be different
    > values; I further guessed that creating and initializing a[4] consumes
    > 4*4bytes to hold the value of a[4] and another 4 byte pointer a to
    > addressing the starting point of the array. But it is not the case in
    > Visual C++ 2005. &a=a in the result. a itself does not have an
    > address.
    >
    > Who can explain this? All comments are welcomed.
    >
    > #include <iostream>
    > using namespace std;
    > void main(){
    > int a[]={1,2,3,4};
    > for (int i=0;i<4;i++){
    > printf("a[%d]:%4d|addr:%6x\n",i,a,&(a));
    > }
    > printf("\n\n");
    > printf("&a: \t%8x\n",&a);
    > printf("a: \t%8x\n",a);
    > printf("*a:\t%8x\n",*a);
    >
    >
    > int *p;
    > p=new int(0xff);
    > printf("&p:\t%8x\n",&p);
    > printf("p:\t%8x\n",p);
    > printf("*p:\t%8x\n",*p);
    > }
    >
    > a[0]: 1|addr:12ff64
    > a[1]: 2|addr:12ff68
    > a[2]: 3|addr:12ff6c
    > a[3]: 4|addr:12ff70
    >
    >
    >
    >
    > &a: 12ff64
    > a: 12ff64
    > *a: 1
    >
    > &p: 12ff60
    > p: 475fd0
    > *p: ff


    Take a look at this article:

    http://www.cplusplus.com/doc/tutorial/pointers.html

    Have nice memory diagrams explaining pointers as well as a section
    regarding your particular question "Pointers and arrays"

    J.
    jalina, Jan 22, 2008
    #2
    1. Advertising

  3. Xiaozhong wrote:
    > However, as is said in many textbook, int a[4] create an array, with
    > the array name 'a' as a pointer to this array.


    Nope. 'a' is not a pointer. 'a' doesn't exist in the executable binary
    and thus doesn't take any space. It's only a name in the source code, a
    kind of "alias" for the array.

    When you *use* the name 'a', the compiler will generate different
    things depending on the context. For example, if a function takes a
    pointer and you give it 'a', the compiler will then create a pointer
    which points to the first element of that array (however, that doesn't
    mean that 'a' itself is a pointer). If you do a "sizeof(a)" then the
    compiler will look how many bytes that array takes and substitute the
    value there.

    The name of the array supports many of the same operations as pointers
    do (which may be a source of confusion). For example "*a" dereferences
    the first element in the array, and "a[n]" dereferences the nth element
    in the array. Just because pointers support the same syntax doesn't mean
    that 'a' is a pointer, though. It just behaves in some ways similarly.

    > I further guessed that creating and initializing a[4] consumes
    > 4*4bytes to hold the value of a[4] and another 4 byte pointer a to
    > addressing the starting point of the array.


    Nope. 'a' doesn't exist and doesn't take any space. It's just a name
    in the source code which can be used to tell the compiler to use that
    array in several ways.
    Juha Nieminen, Jan 22, 2008
    #3
  4. Xiaozhong

    James Kanze Guest

    Re: In (int a[3]; int *p;) is a and p both GENUINE pointers tointegers?

    On Jan 22, 1:57 pm, Juha Nieminen <> wrote:
    > Xiaozhong wrote:
    > > However, as is said in many textbook, int a[4] create an array, with
    > > the array name 'a' as a pointer to this array.


    > Nope. 'a' is not a pointer. 'a' doesn't exist in the executable binary
    > and thus doesn't take any space. It's only a name in the source code, a
    > kind of "alias" for the array.


    In the same way p would be an alias for (or the name of) his
    pointer. I think we can say that 'a' is an array
    (understanding, of course, that names don't exist in the
    executable binary image).

    > When you *use* the name 'a', the compiler will generate different
    > things depending on the context. For example, if a function takes a
    > pointer and you give it 'a', the compiler will then create a pointer
    > which points to the first element of that array (however, that doesn't
    > mean that 'a' itself is a pointer). If you do a "sizeof(a)" then the
    > compiler will look how many bytes that array takes and substitute the
    > value there.


    > The name of the array supports many of the same operations as pointers
    > do (which may be a source of confusion).


    That's not really right. The name of an array supports all of
    the operations which are legal on an array, and only those
    operations. The confusion comes from the fact that an array
    converts implicitly to a pointer in many cases. (Far too many,
    in fact, which is why serious programmers avoid them.)

    > For example "*a" dereferences
    > the first element in the array, and "a[n]" dereferences the nth element
    > in the array. Just because pointers support the same syntax doesn't mean
    > that 'a' is a pointer, though. It just behaves in some ways similarly.


    In the above example, it does mean that the array has been
    implicitly converted to a pointer. Because in C++, the []
    operator isn't defined for array types, only for pointer types
    (or for class types, if the class overloads it).

    (And I know, that sounds stupid. But don't blame me---I didn't
    design the language.)

    > > I further guessed that creating and initializing a[4]
    > > consumes 4*4bytes to hold the value of a[4] and another 4
    > > byte pointer a to addressing the starting point of the
    > > array.


    > Nope. 'a' doesn't exist and doesn't take any space. It's just
    > a name in the source code which can be used to tell the
    > compiler to use that array in several ways.


    It's important to note that the declaration "int a[4]" can have
    different meanings according to context. Usually, it declares
    an array of four ints, but as a function parameter, it declares
    a pointer to an int (and the 4 is just comment, ignored by the
    compiler).

    --
    James Kanze (GABI Software) mailto:
    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, Jan 22, 2008
    #4
  5. Xiaozhong wrote:
    > I recently find int *p is a GENUINE pointer, which means, p itself is
    > an integer that takes 4 bytes in memory;


    Just for completeness, on my machine, int* uses 8 bytes and int uses 4,
    so pointers and ints are not interchangeable. This is a common mistake,
    which I seen often and which I have come across just today in 3rd-party
    code.

    The rest has been explained by others.
    Matthias Buelow, Jan 23, 2008
    #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. smart_mundial
    Replies:
    0
    Views:
    322
    smart_mundial
    Nov 26, 2004
  2. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  3. * Its my Pleasure *
    Replies:
    0
    Views:
    374
    * Its my Pleasure *
    Feb 20, 2008
  4. Jonathan N. Little

    Re: Free Genuine Windows 8

    Jonathan N. Little, Mar 9, 2012, in forum: HTML
    Replies:
    0
    Views:
    419
    Jonathan N. Little
    Mar 9, 2012
  5. Luca Risolia
    Replies:
    1
    Views:
    324
Loading...

Share This Page