Array[n] vs Array[10] - Initializing array with variable vs real number
I am having the following issue with my code:
int n = 10;
double tenorData[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Returns the following error:
error: variable-sized object 'tenorData' may not be initialized
Whereas using double tenorData[10]
works.
Anyone know why?
c++ arrays initialization size
add a comment |
I am having the following issue with my code:
int n = 10;
double tenorData[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Returns the following error:
error: variable-sized object 'tenorData' may not be initialized
Whereas using double tenorData[10]
works.
Anyone know why?
c++ arrays initialization size
4
It would help to give a language. In C++ your arrays of that form need to have a compile-time constant size.
– OrangeAlmondSoap
Feb 21 '13 at 22:04
C++, using Codeblocks with the mingw32-g++ compiler!
– msmf14
Feb 21 '13 at 22:05
Thanks, Justin and @AndrewVarnerin, that solved it! added const before the int: const int n = 10; Solved!
– msmf14
Feb 21 '13 at 22:07
stackoverflow.com/questions/1887097/variable-length-arrays-in-c
– phuclv
Dec 4 '14 at 9:50
add a comment |
I am having the following issue with my code:
int n = 10;
double tenorData[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Returns the following error:
error: variable-sized object 'tenorData' may not be initialized
Whereas using double tenorData[10]
works.
Anyone know why?
c++ arrays initialization size
I am having the following issue with my code:
int n = 10;
double tenorData[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Returns the following error:
error: variable-sized object 'tenorData' may not be initialized
Whereas using double tenorData[10]
works.
Anyone know why?
c++ arrays initialization size
c++ arrays initialization size
edited Oct 17 '16 at 22:53
QuantumFool
3762423
3762423
asked Feb 21 '13 at 22:01
msmf14msmf14
4392817
4392817
4
It would help to give a language. In C++ your arrays of that form need to have a compile-time constant size.
– OrangeAlmondSoap
Feb 21 '13 at 22:04
C++, using Codeblocks with the mingw32-g++ compiler!
– msmf14
Feb 21 '13 at 22:05
Thanks, Justin and @AndrewVarnerin, that solved it! added const before the int: const int n = 10; Solved!
– msmf14
Feb 21 '13 at 22:07
stackoverflow.com/questions/1887097/variable-length-arrays-in-c
– phuclv
Dec 4 '14 at 9:50
add a comment |
4
It would help to give a language. In C++ your arrays of that form need to have a compile-time constant size.
– OrangeAlmondSoap
Feb 21 '13 at 22:04
C++, using Codeblocks with the mingw32-g++ compiler!
– msmf14
Feb 21 '13 at 22:05
Thanks, Justin and @AndrewVarnerin, that solved it! added const before the int: const int n = 10; Solved!
– msmf14
Feb 21 '13 at 22:07
stackoverflow.com/questions/1887097/variable-length-arrays-in-c
– phuclv
Dec 4 '14 at 9:50
4
4
It would help to give a language. In C++ your arrays of that form need to have a compile-time constant size.
– OrangeAlmondSoap
Feb 21 '13 at 22:04
It would help to give a language. In C++ your arrays of that form need to have a compile-time constant size.
– OrangeAlmondSoap
Feb 21 '13 at 22:04
C++, using Codeblocks with the mingw32-g++ compiler!
– msmf14
Feb 21 '13 at 22:05
C++, using Codeblocks with the mingw32-g++ compiler!
– msmf14
Feb 21 '13 at 22:05
Thanks, Justin and @AndrewVarnerin, that solved it! added const before the int: const int n = 10; Solved!
– msmf14
Feb 21 '13 at 22:07
Thanks, Justin and @AndrewVarnerin, that solved it! added const before the int: const int n = 10; Solved!
– msmf14
Feb 21 '13 at 22:07
stackoverflow.com/questions/1887097/variable-length-arrays-in-c
– phuclv
Dec 4 '14 at 9:50
stackoverflow.com/questions/1887097/variable-length-arrays-in-c
– phuclv
Dec 4 '14 at 9:50
add a comment |
1 Answer
1
active
oldest
votes
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic
about following the C++ standard), you can do:
int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:
int n = 10;
double* a = new double[n]; // Don't forget to delete a; when you're done!
Or, better yet, use a standard container:
int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>
If you still want a proper array, you can use a constant, not a variable, when creating it:
const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)
Similarly, if you want to get the size from a function in C++11, you can use a constexpr
:
constexpr int n()
{
return 10;
}
double a[n()]; // n() is a compile time constant expression
1
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
@msmf14: Yeah, standard containers, likevector
, are incredibly useful.
– Cornstalks
Feb 21 '13 at 22:21
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
@Justin: Yes, it value-intializes (or default-constructs forstruct
s/class
es) the elements (soint
s/float
s/etc will all be zero).
– Cornstalks
Mar 24 '14 at 14:22
2
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
|
show 1 more comment
protected by Community♦ Oct 8 '17 at 11:49
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic
about following the C++ standard), you can do:
int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:
int n = 10;
double* a = new double[n]; // Don't forget to delete a; when you're done!
Or, better yet, use a standard container:
int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>
If you still want a proper array, you can use a constant, not a variable, when creating it:
const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)
Similarly, if you want to get the size from a function in C++11, you can use a constexpr
:
constexpr int n()
{
return 10;
}
double a[n()]; // n() is a compile time constant expression
1
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
@msmf14: Yeah, standard containers, likevector
, are incredibly useful.
– Cornstalks
Feb 21 '13 at 22:21
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
@Justin: Yes, it value-intializes (or default-constructs forstruct
s/class
es) the elements (soint
s/float
s/etc will all be zero).
– Cornstalks
Mar 24 '14 at 14:22
2
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
|
show 1 more comment
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic
about following the C++ standard), you can do:
int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:
int n = 10;
double* a = new double[n]; // Don't forget to delete a; when you're done!
Or, better yet, use a standard container:
int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>
If you still want a proper array, you can use a constant, not a variable, when creating it:
const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)
Similarly, if you want to get the size from a function in C++11, you can use a constexpr
:
constexpr int n()
{
return 10;
}
double a[n()]; // n() is a compile time constant expression
1
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
@msmf14: Yeah, standard containers, likevector
, are incredibly useful.
– Cornstalks
Feb 21 '13 at 22:21
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
@Justin: Yes, it value-intializes (or default-constructs forstruct
s/class
es) the elements (soint
s/float
s/etc will all be zero).
– Cornstalks
Mar 24 '14 at 14:22
2
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
|
show 1 more comment
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic
about following the C++ standard), you can do:
int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:
int n = 10;
double* a = new double[n]; // Don't forget to delete a; when you're done!
Or, better yet, use a standard container:
int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>
If you still want a proper array, you can use a constant, not a variable, when creating it:
const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)
Similarly, if you want to get the size from a function in C++11, you can use a constexpr
:
constexpr int n()
{
return 10;
}
double a[n()]; // n() is a compile time constant expression
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic
about following the C++ standard), you can do:
int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:
int n = 10;
double* a = new double[n]; // Don't forget to delete a; when you're done!
Or, better yet, use a standard container:
int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>
If you still want a proper array, you can use a constant, not a variable, when creating it:
const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)
Similarly, if you want to get the size from a function in C++11, you can use a constexpr
:
constexpr int n()
{
return 10;
}
double a[n()]; // n() is a compile time constant expression
edited Aug 27 '16 at 12:18
j123b567
1,60711318
1,60711318
answered Feb 21 '13 at 22:16


CornstalksCornstalks
27.2k1058111
27.2k1058111
1
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
@msmf14: Yeah, standard containers, likevector
, are incredibly useful.
– Cornstalks
Feb 21 '13 at 22:21
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
@Justin: Yes, it value-intializes (or default-constructs forstruct
s/class
es) the elements (soint
s/float
s/etc will all be zero).
– Cornstalks
Mar 24 '14 at 14:22
2
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
|
show 1 more comment
1
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
@msmf14: Yeah, standard containers, likevector
, are incredibly useful.
– Cornstalks
Feb 21 '13 at 22:21
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
@Justin: Yes, it value-intializes (or default-constructs forstruct
s/class
es) the elements (soint
s/float
s/etc will all be zero).
– Cornstalks
Mar 24 '14 at 14:22
2
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
1
1
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
Thank you, this is another good solution. What I really need in the end is a vector rather than an array!
– msmf14
Feb 21 '13 at 22:19
@msmf14: Yeah, standard containers, like
vector
, are incredibly useful.– Cornstalks
Feb 21 '13 at 22:21
@msmf14: Yeah, standard containers, like
vector
, are incredibly useful.– Cornstalks
Feb 21 '13 at 22:21
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
Does the vector solution initialize each element when you call "std::vector<[some class]> a(n);"?
– Justin
Mar 23 '14 at 23:11
@Justin: Yes, it value-intializes (or default-constructs for
struct
s/class
es) the elements (so int
s/float
s/etc will all be zero).– Cornstalks
Mar 24 '14 at 14:22
@Justin: Yes, it value-intializes (or default-constructs for
struct
s/class
es) the elements (so int
s/float
s/etc will all be zero).– Cornstalks
Mar 24 '14 at 14:22
2
2
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
If you're not allocating much (if it's small compared to stack size), I'd prefer using stack memory with alloca(3) and placement new. This way you don't need to worry about free'ing the memory, and memory allocation is much much faster.
– holgac
Apr 4 '16 at 6:59
|
show 1 more comment
protected by Community♦ Oct 8 '17 at 11:49
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
4
It would help to give a language. In C++ your arrays of that form need to have a compile-time constant size.
– OrangeAlmondSoap
Feb 21 '13 at 22:04
C++, using Codeblocks with the mingw32-g++ compiler!
– msmf14
Feb 21 '13 at 22:05
Thanks, Justin and @AndrewVarnerin, that solved it! added const before the int: const int n = 10; Solved!
– msmf14
Feb 21 '13 at 22:07
stackoverflow.com/questions/1887097/variable-length-arrays-in-c
– phuclv
Dec 4 '14 at 9:50