Re: Problem with array objects
"A. Bolmarcich" <aggedor@earl-grey.cloud9.net> wrote in message
news:slrniuqah1.16ra.aggedor@earl-grey.cloud9.net...
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.
This is not a definition of the term "array", this piece of text you have
quoted is not the C++ std's way of saying an array is definitely not an
sequence of objects, this is saying and array TYPE object is ....and it
doesn't mean this is the only possible way to consider an array.
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.
Rubbish the value of a pointer object does not contain any typeinfo. Its a
simple integer value that represents the address of the pointed to object.
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 point to exactly the same location.
The type of a pointer does not define what is pointed to.
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.
This is completely made up. Where does the standard define that a pointer
type defines what is pointed to?
In fact where does the C++ standard define the term "points to"? It doesn't.
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.
No its a pointer to 7 integers.
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.
Accessing what p2 points to also accesses the same integer.
They are just different types of pointer but they both point to exactly the
same thing.
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.
That is the TYPE of pointer, nobody is disputing the pointer TYPE. But a
pointer TYPE does not define what is pointed to by that pointer.
A pointers' type simply tells us that that pointer is capable of addressing
objects of the given type.
For example:
char* cp;
This pointer is capable of addressing char objects, it doesn't point to a
char object.
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
This is not what the Std says. The standard says that each subsctipt will be
calucalated individually.
i has a multiplier of 5xsizeof(int), j has the multiplier of 1xsizeof(int).
- 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.
They have different types, but they both point to the same location.
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.
This is explaining just what I said, if the type of the pointer is T then
derefernecing it addresses a T.
Assuming it points to a valid T type object.
This is not defining what p points to. p points to a location and whatever
is stored there is what is pointed to.
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).
No what is returned from the new expression is a pointer to the first
element *of an array* of integers.
When the pointer is used as a pointer to the array like so:
p[3];
The expression is evaluated as:
*(p+ 3xsizeof(int))
Where p is a pointer to the beginning of the array , and the offset to the
individual element is added.
This pointer is used as a pointer to the array, not to only just one
element. And it can be used as a pointer to the array because it points to
an array.
Yes its TYPE is int*, but this does not mean it can only point to one single
integer. Its type does not define what it points to.
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".
Well you have said that, becuase it is of type int*, it can only point to an
int.
Now you are saying it is undefined what it points to.
I repeat..the pointer type does not define what is pointed to.
You are shortening the term a "pointer type ptr-to-T" to mean the same as a
"pointer to T". But the two terms do not mean the same thing.
One term is specifically describing the pointer type. The other is referring
to whatever is at the location pointed to.
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.
The term "pointer" is not redefined because C++ pointers are typed.
A pointer is defined in the C standard and C++ inherits this defintion. A
pointer of type char* can point to a single char, and array of chars or
nothing at all. This is the view of the C standard and also Bjarne
Stroustrup in one of his C++ books.
With an array like so:
[int][int][int][int][int][int][int][int]
If a pointer 'p' points to the beinning of this array then the expression:
p+1;
points to the second element.
p+2;
points to the third element, and so on.
The pointer 'p' points to an array of integers, not only one single integer.
The TYPE of 'p' can only be int*, because if it were of type int (*)[8] then
p+2 would point away past the array.
The pointer 'p' is used as a pointer to an array of integers, it could not
be used as a pointer to an array if it only pointed to one single object.
It is a different concept from pointing to one single array type object.