Arrays
Author |
Message |
OneOffDriveByPoster
|
Posted: Wed Mar 14, 2007 8:21 pm Post subject: Re: RE:Arrays |
|
|
wtd @ Wed Mar 14, 2007 1:42 pm wrote: It has pointers, and a bit of syntactic sugar for allocating contiguous blocks of memory and getting a pointer to the beginning of said block. It even has compiler tricks that keep track of the length of such blocks of memory, so that it can have syntactic sugar for freeing such blocks of memory.
It does not, however, have arrays as first class type system citizens.
What are your requirements for a language to be considered as having arrays? What of C++ array types? Why not discuss them here? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
haskell
|
Posted: Wed Mar 14, 2007 9:02 pm Post subject: RE:Arrays |
|
|
In C++ arrays are just pointers.
In other languages, arrays are actual structures within the language. In C++, they aren't.
Array is not a type in C++.
In conclusion, C++ array = pointers. There is no discussion to do... Arguing won't change that fact. |
|
|
|
|
 |
OneOffDriveByPoster
|
Posted: Wed Mar 14, 2007 10:57 pm Post subject: Re: RE:Arrays |
|
|
haskell @ Wed Mar 14, 2007 9:02 pm wrote: Array is not a type in C++.
C++ has array types (though "array" itself is not a type in C++). Whether or not they would be considered arrays outside of C++ is another matter. Just because C++ arrays are simple does not mean they do not exist. Pointers and arrays in C++ are not fully interchangable. |
|
|
|
|
 |
md

|
Posted: Thu Mar 15, 2007 12:04 am Post subject: RE:Arrays |
|
|
Pointers and arrays are entirely interchangeable. Ever tried 2[array_var] as a means of accessing the second element? Because it works! Know why it works? Because hte C/C++ compiler just treats it like addition, 2+ array_var or array_var + 2 it still gives the same memory address.
C/C++'s lacking of real arrays can be treated however you want. In some ways it's a pain in the ass; in others it's a boon. However claiming that the language has a feature that it doesn't and most likely never will really doesn't help anyone. |
|
|
|
|
 |
OneOffDriveByPoster
|
Posted: Thu Mar 15, 2007 12:26 am Post subject: Re: RE:Arrays |
|
|
md @ Thu Mar 15, 2007 12:04 am wrote: However claiming that the language has a feature that it doesn't and most likely never will really doesn't help anyone.
That does depend on what you consider to be an array. Also, claiming that a concept of the language does not exist does not help anyone either.
Quote: Pointers and arrays are entirely interchangeable.
Okay, when you say entirely, what do you mean?
c: |
int arr[5];
int *p;
int main(void) {
p = arr;
// arr = p;
}
|
P.S. What are the syntax tags for C++ again? |
|
|
|
|
 |
wtd
|
Posted: Thu Mar 15, 2007 2:48 am Post subject: Re: RE:Arrays |
|
|
OneOffDriveByPoster @ Thu Mar 15, 2007 9:21 am wrote: What of C++ array types? Why not discuss them here?
They do not exist.
On the other hand, to name a few statically-typed, imperative programming languages that do have array types, let's consider Pascal, Ada, Java and C#. |
|
|
|
|
 |
klopyrev
|
Posted: Thu Mar 15, 2007 5:09 am Post subject: Re: Arrays |
|
|
Well, technically, in Java, arrays are pointers as much as they are in C++. Except, in Java, the array is stored with the array size.
KL |
|
|
|
|
 |
haskell
|
Posted: Thu Mar 15, 2007 5:28 am Post subject: RE:Arrays |
|
|
Why bother arguing? Just because you won't believe the truth won't make it go away. If you think in terms of what you see in code all the time, You are basically blind to what is actually going on.
In C++, this is the fact. the undisputable fact, that C++ doesn't have arrays, just pointers. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
OneOffDriveByPoster
|
Posted: Thu Mar 15, 2007 9:32 am Post subject: Re: RE:Arrays |
|
|
haskell @ Thu Mar 15, 2007 5:28 am wrote: If you think in terms of what you see in code all the time, You are basically blind to what is actually going on. Just because the abstraction of arrays in C/C++ is simple (block of contiguous memory) doesn't mean they do not exist. Yes, I know full well that in C++ an array is usually treated as a pointer to its first element. C++ array types have an element type and a size (like many other languages). Yes, the information is pretty much stuck in the compiler (or available at compile-time only), but you can make use of it--(one of wtd's C++ TYS depended on that sort of thing, or at least some solutions do). In the end, it may be that you are the one looking at arrays in other languages and failing to see how they may be implemented. |
|
|
|
|
 |
BenLi

|
Posted: Thu Mar 15, 2007 10:17 am Post subject: RE:Arrays |
|
|
okay OneOffDriveByPoster, why do you feel so passionate about this topic that you would go as far as to argue with people (who im willing to bet are more experienced than you) for two pages. Why does it matter? You're still going to be using "arrays" in C++ like arrays no matter what these guys say. Just leave it. |
|
|
|
|
 |
wtd
|
Posted: Thu Mar 15, 2007 11:00 am Post subject: RE:Arrays |
|
|
This is not a technicality. It's an important, fundamental thing to understand why C++ does not have arrays, and why it cannot be said to possess them, unless implemented as part of a library.
C++ "arrays" do not exist as far as C++'s type system is concerned. There is syntax to support it, and even compiler tricks, but as far as the type system is ultimately concerned, when you declare an "array" of type T, you are simply declaring a pointer of type T.
It cannot then later enforce the difference between an "array" or T and a pointer to T. |
|
|
|
|
 |
haskell
|
Posted: Thu Mar 15, 2007 11:05 am Post subject: RE:Arrays |
|
|
To another end, you are in denial. It IS a pointer. Its not usually. They are one and the same.
Consider:
code: | int main()
{
int array[] = {6};
case_in_point(array);
return 0;
}
void case_in_point(int * array)
{
cout << array[0] << endl;
} |
And also:
code: | int main()
{
int * pointer = NULL;
pointer = new int(6);
case_in_point(pointer);
pointer = NULL;
return 0;
}
void case_in_point(int * pointer)
{
cout << pointer[0] << endl;
} |
They are one and the same.
The "array" is emulated in C++, but it is NOT, in actuality, an array, and there are many possible examples to show this fact. It is 100% functional as an array, but it really isn't. |
|
|
|
|
 |
OneOffDriveByPoster
|
Posted: Thu Mar 15, 2007 11:35 am Post subject: Re: RE:Arrays |
|
|
wtd @ Thu Mar 15, 2007 11:00 am wrote: It cannot then later enforce the difference between an "array" or T and a pointer to T.
code: | int main(void) {
int array[5];
int *p = new int[5];
int **q = &p;
// int **r = &array; // shouldn't compile
return sizeof(array) != 5 * sizeof(int) || sizeof(p) != sizeof(int *); // return 0
}
| Unless if you mean that it can't do it at run-time. |
|
|
|
|
 |
haskell
|
Posted: Thu Mar 15, 2007 12:20 pm Post subject: RE:Arrays |
|
|
Wow, manual memory management is more efficient that using the "standard" array model, which si bigger than it needs to be.
The point under raps is that arrays are just pointers, not that the compiler sucks at managing memory. |
|
|
|
|
 |
wtd
|
Posted: Fri Mar 16, 2007 10:03 am Post subject: Re: RE:Arrays |
|
|
OneOffDriveByPoster @ Fri Mar 16, 2007 12:35 am wrote: wtd @ Thu Mar 15, 2007 11:00 am wrote: It cannot then later enforce the difference between an "array" or T and a pointer to T.
code: | int main(void) {
int array[5];
int *p = new int[5];
int **q = &p;
// int **r = &array; // shouldn't compile
return sizeof(array) != 5 * sizeof(int) || sizeof(p) != sizeof(int *); // return 0
}
| Unless if you mean that it can't do it at run-time.
No. Your code demonstrated very well that it can't distinguish them at compile time either. The example that won't compile illustrates the compiler distinguishing between distinct pointer types. Your previous example demonstrates quite well that it cannot tell the difference between a pointer and an array.
Further, your return has no bearing on this issue. Due to compiler tricks, it can find the size of an array. This means nothing to the type system, however. |
|
|
|
|
 |
|
|