Posted: 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
Sponsor Sponsor
wtd
Posted: 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.
klopyrev
Posted: Fri Mar 02, 2007 4:29 pm Post subject: Re: Arrays
What you wrote sounded like it came exactly out of my book Anyway, thanks for confirming what I thought.
KL
abcdefghijklmnopqrstuvwxy
Posted: Sun Mar 04, 2007 7:07 pm Post subject: RE:Arrays
won't this always work?
code:
int size;
cin >> size;
int array[size];
bugzpodder
Posted: Sun Mar 04, 2007 7:44 pm Post subject: RE:Arrays
no
abcdefghijklmnopqrstuvwxy
Posted: 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?
wtd
Posted: 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.
bugzpodder
Posted: Sun Mar 04, 2007 8:43 pm Post subject: RE:Arrays
although this seem to have worked in gcc 3 or something.
Sponsor Sponsor
OneOffDriveByPoster
Posted: 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.
abcdefghijklmnopqrstuvwxy
Posted: 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:
code:
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
Posted: Wed Mar 07, 2007 1:19 am Post subject: Re: RE:Arrays
abcdefghijklmnopqrstuvwxy @ Tue Mar 06, 2007 11:56 pm wrote:
code:
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
Posted: Wed Mar 07, 2007 1:17 pm Post subject: RE:Arrays
EDIT: By the way "OneOffDriveByPoster" your character inspires me to laugh!! Drive-by-poster, honestly. HAHA
ownageprince
Posted: Tue Mar 13, 2007 10:30 am Post subject: Re: Arrays
just use the vector library.
OneOffDriveByPoster
Posted: 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.
...
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?
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"...
wtd
Posted: 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.
...
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.