Re: Unsigned types are DANGEROUS??
On Tue, 15 Mar 2011 20:14:12 -0000, "Paul" <pchristor@yahoo.co.uk>
wrote:
"Peter Remmers" <p.remmers@expires-2011-03-31.arcornews.de> wrote
in message
news:4d7fb548$0$7653$9b4e6d93@newsspool1.arcor-online.net...
Am 15.03.2011 19:17, schrieb Paul:
"Noah Roberts"<dont@email.me> wrote in message
news:4d7f92bd$0$2403$cc2e38e6@news.uslec.net...
The important, practical consequence is that array size
information is
lost as soon as the array identifier is converted into a
pointer. You
can't even get it back when the information is still available
to the
compiler:
int arr[] = {1,2,3};
int * p = arr;
sizeof(arr) == 12
sizeof(p) == 4
With your interpretation a dynamic array is not an array, which
is
obvioulsy
incorrect.
An array created by 'new' is an array that has no name. Just like
with
everything that is dynamically created by new, there is no
identifier that
directly identifies the object created on the heap. All you get
is a
pointer to the object which must be stored in a pointer variable
that you
have to provide. That pointer is not the array itself. It is a
different
object, with its own identifier and its own region of storage.
This is
obvious if you consider what sizeof() returns for the two.
Consider this:
new int[3];
This statement does not contain any identifiers, and yet an array
has been
created on the heap. And because the pointer that new returned
was not
saved, the array is leaked. There is just no way to get hold of
the array
without an identifier. A pointer only gives you indirect access
to the
array.
The pointer is the arrays identifier.
What do you mean by , " it only gives indirect access"?
It's the only way to access the array, at all.
The array identifier carries the information about the array's
number of
elements and their type. A pointer to one of those elements does
not.
If you have a recent Microsoft compiler at your disposal, take a
look at
how _countof() is implemented, which returns the number of
elements in
array. It uses some template magic to deliberately fail at
compile-time if
you try to pass it a pointer instead of a real array identifier.
The difference between an array and a pointer can't be any more
obvious.
What you mean is the difference between an array-type and
pointer-type,
can't be more obvious.
I think what you are trying to explain is the following:
#include <iostream>
typedef int t_arr16[16];
void foo1(int* par){
std::cout<< "sizeof:"<< sizeof(par)<< "\t typeinfo:"<<
typeid(par).name();}
void foo2(t_arr16& par){
std::cout<< "sizeof:"<< sizeof(par)<< "\t typeinfo:"<<
typeid(par).name();}
int main(){
t_arr16 arr1 ={0};
std::cout<< "Calling foo1: ";
foo1(arr1);
std::cout<< std::endl << "Calling foo2: ";
foo2(arr1);
}
This does not mean the following is not an array:
int* arr1 = new int[16];
++arr1;
And it doesn't suggest an array must always have zero based
indexing. The
C++ standard specifically states the rules about array indexing and
its
clear that C++ allows non-zero based indexing.
What possible reason, even in the most idiotic minds, would there
be to
restrict the language in such a way?
Playing word games can work both ways, example:
int x;
The actual integer object is just a a piece of memory, x is just an
identifier. So are we wrong to say x is an integer? Likewise:
int* arr = new int[16];
The actual array object is just a piece of memory, arr is just an
identifier. So are we wrong to say arr is an array?
int x; /* x is an integer */
int * p = &x; /* p is a pointer to an integer */
int a[3]; /* a is an array of 3 integers */
int * pa = a; /* pa is a pointer to an integer */
int * dx = new int(); /* dx is a pointer to an integer */
int * da = new int[3]; /* da is a pointer to an integer */
Because 'pa' and 'da' both point to the first element of an array, it
is well defined to do pointer arithmatic on them as long as you stay
within the bounds of the underlying array (e.g 'pa[1]' which is
equivalent to '*(pa + 1)'). With 'a', 'a[1]' is again equivalent '*(a
+ 1)'. However, since a is actually an array, a is implicitly
converted to type int* before operator+ can be applied, so it is
equivalent to '*((int*)a + 1)'.
It's up to you how you want to look at it, I think my views are
quite clear,
and I'm happy with my view on the subject.