
-----------------------------------
klopyrev
Thu Mar 01, 2007 10:57 pm

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

-----------------------------------
wtd
Fri Mar 02, 2007 3:12 pm

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.

-----------------------------------
klopyrev
Fri Mar 02, 2007 4:29 pm

Re: Arrays
-----------------------------------
What you wrote sounded like it came exactly out of my book :P Anyway, thanks for confirming what I thought.

KL

-----------------------------------
abcdefghijklmnopqrstuvwxy
Sun Mar 04, 2007 7:07 pm

RE:Arrays
-----------------------------------
won't this always work?  


int size;

cin >> size;

int array[size];


-----------------------------------
bugzpodder
Sun Mar 04, 2007 7:44 pm

RE:Arrays
-----------------------------------
no

-----------------------------------
abcdefghijklmnopqrstuvwxy
Sun Mar 04, 2007 8:14 pm

RE:Arrays
-----------------------------------
so if the user always entered 5 for the size, it wouldn't always work?  Why not?

-----------------------------------
wtd
Sun Mar 04, 2007 8:26 pm

RE:Arrays
-----------------------------------
The compiler cannot know, when it compiles the code, what the user will enter.

-----------------------------------
bugzpodder
Sun Mar 04, 2007 8:43 pm

RE:Arrays
-----------------------------------
although this seem to have worked in gcc 3 or something.

-----------------------------------
OneOffDriveByPoster
Tue Mar 06, 2007 11:31 pm

Re: RE:Arrays
-----------------------------------
although this seem to have worked in gcc 3 or something.

Variable length arrays.  GCC supports it in their C++ implementation too.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Tue Mar 06, 2007 11:56 pm

RE:Arrays
-----------------------------------
I'm pretty sure that with proper type checking + size checking it will always work as expected.  

for instance:
 

int me = 0;
while (me == 0) {
cin >> me; 
  if (me != int || me > 100 || me < 1) {
    me = 0; 
    continue;
  }
}
int array[me];


I know me != int is invalid, but in the spirit of psuedo I hope you know what this means.

-----------------------------------
OneOffDriveByPoster
Wed Mar 07, 2007 1:19 am

Re: RE:Arrays
-----------------------------------

int me = 0;
while (me == 0) {
cin >> me; 
  if (me != int || me > 100 || me < 1) {
    me = 0; 
    continue;
  }
}
int array[me];

With a compiler extension, I guess (clearly C++ intended)...

-----------------------------------
abcdefghijklmnopqrstuvwxy
Wed Mar 07, 2007 1:17 pm

RE:Arrays
-----------------------------------
:D  EDIT: By the way "OneOffDriveByPoster" your character inspires me to laugh!! Drive-by-poster, honestly.  HAHA

-----------------------------------
ownageprince
Tue Mar 13, 2007 10:30 am

Re: Arrays
-----------------------------------
just use the vector library.

-----------------------------------
OneOffDriveByPoster
Tue Mar 13, 2007 10:54 pm

Re: RE:Arrays
-----------------------------------
First off, realize 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.
There are reasons to distinguish between arrays and pointers in C++.  Why say that C++ has no arrays?

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"...

-----------------------------------
wtd
Wed Mar 14, 2007 1:42 pm

Re: RE:Arrays
-----------------------------------
First off, realize 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.
There are reasons to distinguish between arrays and pointers in C++.  Why say that C++ has no arrays?

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.

-----------------------------------
OneOffDriveByPoster
Wed Mar 14, 2007 8:21 pm

Re: RE:Arrays
-----------------------------------
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?

-----------------------------------
haskell
Wed Mar 14, 2007 9:02 pm

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
Wed Mar 14, 2007 10:57 pm

Re: RE:Arrays
-----------------------------------
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
Thu Mar 15, 2007 12:04 am

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
Thu Mar 15, 2007 12:26 am

Re: RE:Arrays
-----------------------------------
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.

Pointers and arrays are entirely interchangeable.
Okay, when you say entirely, what do you mean?

int arr
P.S.  What are the syntax tags for C++ again?

-----------------------------------
wtd
Thu Mar 15, 2007 2:48 am

Re: RE:Arrays
-----------------------------------
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
Thu Mar 15, 2007 5:09 am

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
Thu Mar 15, 2007 5:28 am

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.

-----------------------------------
OneOffDriveByPoster
Thu Mar 15, 2007 9:32 am

Re: RE:Arrays
-----------------------------------
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
Thu Mar 15, 2007 10:17 am

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
Thu Mar 15, 2007 11:00 am

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
Thu Mar 15, 2007 11:05 am

RE:Arrays
-----------------------------------
To another end, you are in denial. It IS a pointer. Its not usually. They are one and the same. 

 Consider:

int main()
{
    int array[] = {6};

    case_in_point(array);
    
    return 0;
}

void case_in_point(int * array)
{
   cout 