Computer Science Canada Arrays |
Author: | klopyrev [ Thu Mar 01, 2007 10:57 pm ] |
Post subject: | Arrays |
Something about C++ is confusing me at the moment. It concerns arrays. I started reading about arrays and it told me that to create an array, you have to use code like this: int array [5]; where the size of the array has to be a constant. I also read that if you want to create a dynamic array, you have to do something like this. int * array = new int[N]; I got used to this notion, but then I tried this code. int N; cin >> N; int array[N]; I could use that array without any errors, runtime or syntax. Having read that the size has to be a constant, I don't understand why that code would work. Is that something compiler specific or can I always use it? Is the difference between creating arrays in these two different methods only the type of memory used to store each? KL |
Author: | wtd [ Fri Mar 02, 2007 3:12 pm ] |
Post subject: | RE:Arrays |
First off, realize that C++ has no arrays. There is no spoon, Neo. C++ has pointers. An array is a pointer to the beginning of a block of memory where a bunch of values are stored sequentially. The two forms you show differ in their means of allocation. In the former case, the memory is allocated statically, on the stack. This means that it cannot be accessed from outer scopes, and it must be of a constant size. Due to the nature of stack allocation, these automatically have their memory "freed" when they go out of scope. Arrays allocated dynamically have memory allocated on the heap. They may be initialized with a variable size, and a pointer to them may safely be passed to an outer scope, as they are not automatically freed. As a result, they must also be explicitly deleted when no longer wanted. If the code you wrote worked, then congrats, but do not rely on this behavior. |
Author: | klopyrev [ Fri Mar 02, 2007 4:29 pm ] |
Post subject: | Re: Arrays |
What you wrote sounded like it came exactly out of my book ![]() KL |
Author: | abcdefghijklmnopqrstuvwxy [ Sun Mar 04, 2007 7:07 pm ] | ||
Post subject: | RE:Arrays | ||
won't this always work?
|
Author: | bugzpodder [ Sun Mar 04, 2007 7:44 pm ] |
Post subject: | RE:Arrays |
no |
Author: | abcdefghijklmnopqrstuvwxy [ Sun Mar 04, 2007 8:14 pm ] |
Post subject: | RE:Arrays |
so if the user always entered 5 for the size, it wouldn't always work? Why not? |
Author: | wtd [ Sun Mar 04, 2007 8:26 pm ] |
Post subject: | RE:Arrays |
The compiler cannot know, when it compiles the code, what the user will enter. |
Author: | bugzpodder [ Sun Mar 04, 2007 8:43 pm ] |
Post subject: | RE:Arrays |
although this seem to have worked in gcc 3 or something. |
Author: | OneOffDriveByPoster [ Tue Mar 06, 2007 11:31 pm ] |
Post subject: | Re: RE:Arrays |
bugzpodder @ Sun Mar 04, 2007 8:43 pm wrote: although this seem to have worked in gcc 3 or something.
Variable length arrays. GCC supports it in their C++ implementation too. |
Author: | abcdefghijklmnopqrstuvwxy [ Tue Mar 06, 2007 11:56 pm ] | ||
Post subject: | RE:Arrays | ||
I'm pretty sure that with proper type checking + size checking it will always work as expected. for instance:
I know me != int is invalid, but in the spirit of psuedo I hope you know what this means. |
Author: | OneOffDriveByPoster [ Wed Mar 07, 2007 1:19 am ] | ||
Post subject: | Re: RE:Arrays | ||
abcdefghijklmnopqrstuvwxy @ Tue Mar 06, 2007 11:56 pm wrote:
With a compiler extension, I guess (clearly C++ intended)... |
Author: | abcdefghijklmnopqrstuvwxy [ Wed Mar 07, 2007 1:17 pm ] |
Post subject: | RE:Arrays |
![]() |
Author: | ownageprince [ Tue Mar 13, 2007 10:30 am ] |
Post subject: | Re: Arrays |
just use the vector library. |
Author: | OneOffDriveByPoster [ Tue Mar 13, 2007 10:54 pm ] |
Post subject: | Re: RE:Arrays |
wtd @ Fri Mar 02, 2007 3:12 pm wrote: First off, realize that C++ has no arrays.
There are reasons to distinguish between arrays and pointers in C++. Why say that C++ has no arrays?
... C++ has pointers. An array is a pointer to the beginning of a block of memory where a bunch of values are stored sequentially. Quote: The two forms you show differ in their means of allocation. In the former case, the memory is allocated statically, on the stack. This means that it cannot be accessed from outer scopes, and it must be of a constant size.
I hope you mean "automatically, on the stack" or "statically or on the stack". Not that your statement is categorically wrong--but using "static"... |
Author: | wtd [ Wed Mar 14, 2007 1:42 pm ] |
Post subject: | Re: RE:Arrays |
OneOffDriveByPoster @ Wed Mar 14, 2007 11:54 am wrote: wtd @ Fri Mar 02, 2007 3:12 pm wrote: First off, realize that C++ has no arrays.
There are reasons to distinguish between arrays and pointers in C++. Why say that C++ has no arrays?... C++ has pointers. An array is a pointer to the beginning of a block of memory where a bunch of values are stored sequentially. Because it doesn't? 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. |
Author: | OneOffDriveByPoster [ 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.
What are your requirements for a language to be considered as having arrays? What of C++ array types? Why not discuss them here?It does not, however, have arrays as first class type system citizens. |
Author: | haskell [ 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. |
Author: | OneOffDriveByPoster [ 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. |
Author: | md [ 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. |
Author: | OneOffDriveByPoster [ 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?
P.S. What are the syntax tags for C++ again? |
Author: | wtd [ 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#. |
Author: | klopyrev [ 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 |
Author: | haskell [ 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. |
Author: | OneOffDriveByPoster [ 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. |
Author: | BenLi [ 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. |
Author: | wtd [ 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. |
Author: | haskell [ 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:
And also:
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. |
Author: | OneOffDriveByPoster [ 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.
|
Author: | haskell [ 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. |
Author: | wtd [ 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.
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. |
Author: | md [ Fri Mar 16, 2007 9:42 pm ] |
Post subject: | RE:Arrays |
wtd you're a semantic Nazi! I now invoke Godwin's law and call an end to this thread. |
Author: | abcdefghijklmnopqrstuvwxy [ Tue Mar 20, 2007 1:44 am ] |
Post subject: | RE:Arrays |
What is the difference between a good working library that implements an array type and it being built into the language aside from accessibility? It seems to me that C++ supports pointers to contiguous blocks of memory which we call arrays, and that C# implements an "array type". What is wrong with calling "pointers to contiguous blocks of memory" an array? Nothing. What is wrong with saying C++ supports a native array type? It is false. Drivebyposter didn't say anything false though... |