Re: Problem with array objects
On 2011-06-04, Paul <pchristor@yahoo.co.uk> wrote:
"A. Bolmarcich" <aggedor@earl-grey.cloud9.net> wrote in message
news:slrniukoa8.1fj5.aggedor@earl-grey.cloud9.net...
On 2011-06-03, Paul <pchristor@yahoo.co.uk> wrote:
[snip]
The C++ sees it as: "An object of array type contains a
contiguously allocated nonempty set of N sub-objects of type T."
Pointing to an array is pointing to an object of array type.
Pointing to a sub-object in an array is pointing to something other
than an object of the array type of the array that the sub-object is
in.
You have quoted some text from that seems to describe an object of array
type.
As I said , and you have proved me correct, you can only see an array as "a
single array type object". You cannot see it at as array of integer
objects( or whatever type it is).
I have quoted the C++ standard. Seeing it as the C++ standard does
is the proper way to see it when discussing C++, such as in the
comp.lang.c++ newsgroup. Seeing it another way is seeing it in a
way different from the C++ standard.
With the following code..
int arr[7];
int* p1 = arr;
int (*p2)[7] = &arr;
Both the pointers point-to the first element in the array, and both
pointers
also point-to the array of integer objects. They are just different types
of
pointers.
They both point to exactly the same location.
Both pointers do not point to the first element of the array. p1
points to the first element of the array; p2 points to the array.
An array is not the same as its first element.
The address of an array is the same as the address of the first element, and
a pointer is this address.
The first element of an array is at the same memory location as the
array. The value of a C++ pointer is not only a memory location; it
is a typed memory location. Ignoring their types, the p1 and p2 have
the same value; however because p1 and p2 have different types, they
do not point to the same entity. p1 points to the first element of
the array, not to the array; p2 points to the array.
They both point to exactly the same place and they both point to exactly the
same array. Whether you view it as pointing to an array of pointing to the
first element is just a POV.
They do not both point to exactly the same array. One points to an
array; the other may point to an element of that array. The point of
view of the C++ standard is that a
int (*)[]
points to an array of int while a
int *
points to an int, the int it points to may be an element in an array
of int.
A example of both pointers accessing...;
a) A single object:
*p1;
**p2;
b) An array of obects:
*p2;
for(int i=0; i<7 ; ++i){ p1[i];}
Note that in (b) p1 does not access the integer objects it just accesses
the
array type object. p2 does access the array of integer objects but it
does
not access the array type object.
They only differ, because of their pointer types, in the way they access
the
array. They both point to exactly the same array but in this different
way.
p1 in (b) does not access an array type object. p1 is a pointer to
int. The expression p1[i] is equivalent to *(p1+i). According to
the C++ standard (start of paragraph 5 of 5.7 (Additive operators)):
No p1 is not a pointer to integer(singular) , it is a pointer to
integers(plural).
Correct p1 doesn't access the array-type object , but it accesses the array
of integer objects.
The declarations were
int arr[7];
int* p1 = arr;
int (*p2)[7] = &arr;
p1 is a pointer to an integer. What C++ has available as a pointer to
multiple integers is a pointer to an array of integers, such as p2.
p1 is a pointer to an integer, accessing what p1 points to access
that integer.
When an expression that has integral type is added to or subtracted
from a pointer, the result has the type of the pointer operand. If
the pointer operand points to an element of an array object, and
the array is large enough, the result points to an element offset
from the original element such that the difference of the
subscripts of the resulting and original array elements equals the
integral expression.
The result of (p1+i) is a pointer to int that points to the i-th
element from the array element that p1 points to. p1 points to an
array element; it does not point to the array that the element is in.
Look at the expression :
p1 + i;
It's a pointer plus offset(or index).
p1 is pointer to the beginning of the array
i is an offest that indexes the array.
It cannot be any clearer that p1 points to the array.
An element in an array has a different type than the array the
element is in. The type of p1 is int*. When p1 points to an element
of an array of int, it is pointing to an an int, not to an array.
The type of a pointer to an array of int is of the form int(*)[].
The C++ standard is clear about this in the following example from
8.1 (Types).
int // int i
int * // int *pi
int *[3] // int *p[3]
int (*)[3] // int (*p3i)[3]
int *() // int *f()
int (*)(double) // int (*pf)(double)
name respectively the types "int," "pointer to int," "array of 3
pointers to int," "pointer to array of 3 int," "function of (no
parameters) returning pointer to int," and "pointer to a function
of (double) returning int.
The distinction between a pointer to an array and a pointer to an
element of that array is important for multidimensional arrays as
arrays of arrays in C++. The declaration
int x[3][5];
declares an array of 3 array of 5 int. The expression x[i][j] is
equivalent to *(*(x+i)+j). When that expression is evaluated
- x is an array of 3 array of 5 int
- array-to-pointer conversion results in a pointer to the first array
of 5 int in x
- adding i results in a pointer to the i-th array of 5 int in x
- dereferencing that results in the i-th array of 5 int in x
- array-to-pointer conversion results in a pointer to the first int in
the i-th array of 5 int in x
- adding j results in a pointer to the j-th int in the i-th array of
5 int in x
- dereferencing that result in the j-th int in the i-th array of 5 int
in x
Multidimensional arrays in C++ depend on the distinction between a
pointer to an array and a pointer to an element of that array. Not
understanding this distinction may lead to confusion.
Unlike p1, p2 is a pointer to an array type object. p2 points to an
array. p1 does not point to an array; it points to the first
element of the array that p2 points to.
p1 and p2 point to exactly the same place, and the same array.
p1 and p2 have different types. p1 points to an int, not to an
array. p2 points to an array of 7 int.
Consider a stack frame pointer, pointing to the stack. It doesn't
actually
point the whole stack at the same time but nonetheless its still
considered
to be pointing to the stack.
There is a significant difference between a stack frame pointer and a
C++ pointer: a C++ pointer is typed. In C++, the result of
dereferencing a pointer to T, where T is a type, is a T.
No, dereferencing a pointer of type pointer-to-T, will result in a T if it
points to a valid T object.
The pointer type declares a pointer object that can address an object of the
decalred type , for example:
int *p = 0;
p = &ix; /*assume x is valid int*/
p = new int[64];
What I have written is about what a C++ compiler does, along the
lines of what the C++ standard does in the folliwng sentence about
the unary * operator: "If the type of the expression is pointer to T,
the type of the result is T." These post are long enough without
adding possible undefined behaviors when the program runs due to the
value of a pointer to a non-polymorphic type.
Here the pointer p is declared to be a pointer of type ptr-to-int, it means
this pointer must be capable of pointing to a valid int object., It has
nothing to do with what it points to.
The above the pointer is shown pointing to nothing, an integer and an array
of integers.
What is shown is p having the null pointer value, pointing to an
integer, and pointing to the first integer in an array of integers
(the result of the expression (new int[64]) is a pointer to int,
not a pointer to array of int).
Now consider the following code:
int *p;
What does this point to? Uninitialised memory that could contain anything.
The pointer type only tells us that the declared pointer object is capable
of pointing to an integer, it does not define what is pointed to, as you
have tried to imply.
It is undefined what p points to. I have used shorter phrases, such
as "is a T", rather than a longer ones, such as "is of type T". You
are misreading "is a T" as "is an object of type T that exists when
the program runs".
The result of using a stack frame pointer as an address operand
depends on what the operator does. Different operators move 1, 2, 4,
8, or some other number of bytes to or from storage starting at the
address given by the address operand.
What you seem to be talking about here is what I know as the PTR keyword in
assembly, it may change in different assemblers.
I don't really see what this has to do with the fundamental concept that a
SFP "points to" a stack frame.
In that case, you don't really see the difference between a value
with the correct number of bits to be used as an address in assembler
and a C++ pointer. C++ pointers are typed.