Assigning size of array entered by user to array [duplicate] - c++

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 8 years ago.
Why I can't use this code when I want to assign the size of array entered by user to array?
int n;
cin>>n;
int array[n];
And is there another way of doing this instead of using this construction?
int n;
cin>>n;
int *array;
array = new int[n];

According to O'Reilly "C++ In A Nutshell" (2003),
An array is specified with a constant size in square brackets
Since your variable n is not a constant, it can't be used to specify the size of the array.
The same paragraph also says,
For an array-like container whose size can change at runtime, see <vector> in Chapter 13.
Sorry, but you are not allowed to have this construction.

Related

Why can I initialize the size of an array by taking user input in C++? [duplicate]

This question already has answers here:
Are variable length arrays there in c++?
(2 answers)
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 1 year ago.
Given that user input is taken at runtime but array is created during compile time. How and why does this piece of code work?
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n]; //Why can I use this statement to create an array?
}
It's a non-standard extension, supported by gcc and clang.
It works by allocating space on the stack at the point the array is declared, a bit like alloca.
The memory allocated is automatically freed (by adjusting the stack pointer) when the function returns (or arr goes out of scope).

How to get variable length array [duplicate]

This question already has answers here:
C++ : Creating an array with a size entered by the user
(3 answers)
How to create a dynamic array of integers
(8 answers)
Closed 1 year ago.
I know variable length arrays are not allowed in c++. My code is currently as follows:
int main(){
int t;
cin >> t;
double h[t];
}
How can I create an array with length t then?
Edit: the assignment only allows char, bool, int and double. Vector isn't allowed. When I tried to compile it, it says ISO C++ forbids variable length array 'h'.

How to create a 2D array in C++? [duplicate]

This question already has answers here:
how to create a contiguous 2d array in c++?
(7 answers)
Closed 3 years ago.
OK, this question seems to be silly but bear with me. When I trying to create a 2D array in C++, it gave me some warnings (len is an integer):
double a[len][len];
// warning: variable length arrays are a C99 feature
// warning: variable length array used
So I tried another:
double **a = new double[len][len];
// error: only the first dimension of an allocated array may have dynamic size
// read of non-const variable 'len' is not allowed in a constant expression
How can I do it correctly in C++11?
double** a=new double*[len];
for(int i=0;i<len;++i)
{
a[i]=new double[len];
}
Are there any restrictions on what you can use? If you're planning to do array manipulations I'd say just use [Eigen] (http://eigen.tuxfamily.org/index.php?title=Main_Page)

How to create an array with number of elements from a variable? [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 10 years ago.
I have an integer num that was read from a file. I want to create an array with the number of elements being num.
A sample code of what I want to do but doesn't work:
int num;
cin >> num;
int iarray[num];
Arrays in C++ have compile-time bounds.
Use dynamic allocation instead, or a healthy std::vector wrapper around the same process.
dynamic allocation being int * iarray = new int[num];
Just make sure to call delete[] iarray; at some point to free the memory.

length of dynamic array in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I declared a dynamic array like this:
int *arr = new int[n]; //n is entered by user
Then used this to find length of array:
int len = sizeof(arr)/sizeof(int);
It gives len as 1 instead of n . Why is it so?
Because sizeof does not work for dynamic arrays. It gives you the size of pointer, since int *arr is a pointer
You should store the size of allocated array or better use std::vector
Because arr is not an array, but a pointer, and you are running on an architecture where size of pointer is equal to the size of int.
Andrew is right.
You have to save n somewhere (depends on where do you use it). Or if you are using .NET you could use Array or List...