This question already has answers here:
How to create an array when the size is a variable not a constant?
(6 answers)
Closed 3 years ago.
How to use the the value stored in a int variable as the size of my array
eg.
int a = 40;
int b[a]; // a = 40
You can't; not in standard C++ anyway, unless a is a constexpr or const integral type with a positive value.
The best alternative is a std::vector<int> b(a);
Create an array with new[]:
int *b = new int[a];
but remember to delete it later with:
delete[] b;
A better alternative is the std container, like std::vector.
Related
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)
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 16 days ago.
void method(string a) {
int n = a.size();
int array[n];
}
The above code can compile correctly using gcc. How can the size of the array come from a non-constant variable? Does the compiler automatically translate the int array[n] to int* array = new int[n]?
How can the size of the array come from a non-constant variable?
Currently, because that compiler has a non-standard extension which allows you to use C's variable length arrays in C++ programs.
Does the compiler automatically translate the int array[n] to int* array = new int[n]?
That's an implementation detail. I believe GCC places it on the stack, like normal automatic variables. It may or may not use dynamic allocation if the size is too large for the stack; I don't know myself.
dynamic allocation. The new keyword will do this with a pointer and some allocation.
int * ptr;
int n = a.size();
ptr = new int[n];
According to this the compiler allows this expression in C++ as far as C90/99.
This question already has answers here:
When should I use the new keyword in C++?
(12 answers)
Closed 7 years ago.
Is there any difference between these 2 ways of storing an integer?
int X = 100;
and
int *pX = new int(100);
"Is there any difference between these 2 ways of storing an integer?"
Yes, there is a significant difference.
int X = 100;
Initializes a variable X on the stack with the value 100, while
int *pX = new int(100);
allocates memory for an int on the heap, kept in pointer pX, and initializes the value to 100.
For the latter you should notice, that it's necessary to deallocate that heap memory, when it's no longer needed:
delete pX;
The first one is creating a variable on the stack while the second one is creating a variable on the heap and creating a pointer to point at it.
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.
This question already has answers here:
Create 2D array with "new"? [duplicate]
(3 answers)
Closed 8 years ago.
static int (*g_data)[3];
I'd like to new N elements of int[3]. I'm only able to this as follows:
g_data = (int(*)[3]) new int[N*3];
I know that this is okay and using struct would be an alternative solution. But, just for curiosity, can I directly call new for int[3], i.e., without the type conversion?
Since g_data is a pointer to a 1D array of 3 int, to make an array of N such arrays of 3, you simply use int [N][3]:
g_data = new int[N][3];
You can do it this way:
static int (*g_data)[3];
typedef int int_array[3];
g_data = new int_array[N];