Dynamic arrays ad use of 'new'

Hello,

If in my program I am going to read a number n and will require an integer array with n elements, what is to stop using this piece of code rather than using the 'new' operator? Is it just that all variables must be declared at the beginning of the program?

1
2
3
int n;
cin>>n;
int array[n];


The code above is not C++
closed account (z05DSL3A)
what is to stop using this piece of code rather than using the 'new' operator?

The dimension of the array must be a constant expression. A non-const or const variable that has a value that is unknown until run time cannot be used for the dimension of an array.
Last edited on
matthew1686 wrote:
what is to stop using this piece of code rather than using the 'new' operator?
Yes:
1
2
3
4
5
int n;
cin>>n;
int *array = new int[n];
...
delete[] array; // note: delete an array requires [] after delete 


matthew1686 wrote:
Is it just that all variables must be declared at the beginning of the program?
No. Declare them where they're needed for C++
Bazzy:
The code above is not C++

Do you mean that its not 'correct' C++?

Grey Wolf:
The dimension of the array must be a constant expression. A non-const or const variable that has a value that is unknown until run time cannot be used for the dimension of an array.


Thanks very much, that's helpful to know. Is that due to reasons associated with memory allocation?

And code777, thanks for that code. I'm just having a bit of trouble understanding why the output of the 'new' operator has to be a pointer rather than just a variable. I have put together some code (which seems to work), where I've done something like:

1
2
3
4
5
6
7
8
int n, i;
cin>>n;
int *array = new int[n];

for (i=0; i<n; ++i)
{cin>>array[i];}

delete[] array; 


But isn't array a pointer, so the code should be something like the following for it to work:

1
2
3
4
5
6
7
8
int n, i;
cin>>n;
int *array = new int[n];

for (i=0; i<n; ++i)
{cin>>array*[i];}

delete[] array; 


That is, 'cin' in line 6 should write to the address pointed to by the ith element in array?
Do you mean that its not 'correct' C++?

Yes, I missed valid when I typed
Okay, let's see if I can explain this.

Arrays are a lot like pointers. What's created at line 3 is an array, not an array of pointers (new returns a pointer), so there's no need to dereference twice ([ ] acts a lot like a dereference operator, as it seems). In fact, attempting to compile that code will result in an error, whether you put the * before or after array (although the error varies).

Do you see why now?

-Albatross
Last edited on
Albatross,

Thanks very much for that. I think the key bit of information I was missing is that arrays in C++ are like pointers, which are an entirely new concept to me.
I still can't really understand why pointers are useful. If I need to make a variable accessible to a number of functions I don't see why I can't just use global variables. A computer scientist told me that this gets confusing, but I don't see why keeping track of a number of variables is any more confusing than having to keep track of a load of addresses. Looks like I have a lot more reading to do!

Thanks for your help,
Matt
Pointers have their uses for creating "links" between values or for easily allocating a block of memory of varying sizes, but they also become considerably more useful when you start dealing with polymorphism. Trust me, they're useful, just maybe not now. :)

Also, in C++, it's a good coding habit to make sure that everything is in as small a scope as possible (almost, anyway). Global variables can start to be a problem when you start creating very complex programs with lots of variables, and he/she was right; they can get confusing. With passing by reference (as I assume that's what you meant by "accessible to a number of functions"), not only is the scope and confusion problem reduced, but your code is also a lot more reusable, and reusability is, to put it in my words, awesome. :P

-Albatross
Topic archived. No new replies allowed.