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

X

Xiaozhong

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
 
J

jalina

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.
 
J

Juha Nieminen

Xiaozhong said:
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.
 
J

James Kanze

Xiaozhong said:
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).
 
M

Matthias Buelow

Xiaozhong said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top