Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrays
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
klopyrev




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: Fri Mar 02, 2007 4:29 pm   Post subject: Re: Arrays

What you wrote sounded like it came exactly out of my book Razz Anyway, thanks for confirming what I thought.

KL
abcdefghijklmnopqrstuvwxy




PostPosted: 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




PostPosted: Sun Mar 04, 2007 7:44 pm   Post subject: RE:Arrays

no
abcdefghijklmnopqrstuvwxy




PostPosted: 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




PostPosted: 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




PostPosted: Sun Mar 04, 2007 8:43 pm   Post subject: RE:Arrays

although this seem to have worked in gcc 3 or something.
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Wed Mar 07, 2007 1:17 pm   Post subject: RE:Arrays

Very Happy EDIT: By the way "OneOffDriveByPoster" your character inspires me to laugh!! Drive-by-poster, honestly. HAHA
ownageprince




PostPosted: Tue Mar 13, 2007 10:30 am   Post subject: Re: Arrays

just use the vector library.
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 32 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: