Array[n] vs Array[10] - Initializing array with variable vs real number












55















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?










share|improve this question




















  • 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
















55















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?










share|improve this question




















  • 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














55












55








55


29






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















134














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





share|improve this answer





















  • 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, 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











  • @Justin: Yes, it value-intializes (or default-constructs for structs/classes) the elements (so ints/floats/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










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









134














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





share|improve this answer





















  • 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, 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











  • @Justin: Yes, it value-intializes (or default-constructs for structs/classes) the elements (so ints/floats/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
















134














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





share|improve this answer





















  • 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, 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











  • @Justin: Yes, it value-intializes (or default-constructs for structs/classes) the elements (so ints/floats/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














134












134








134







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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, 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











  • @Justin: Yes, it value-intializes (or default-constructs for structs/classes) the elements (so ints/floats/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





    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











  • 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 structs/classes) the elements (so ints/floats/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 structs/classes) the elements (so ints/floats/etc will all be zero).

– Cornstalks
Mar 24 '14 at 14:22





@Justin: Yes, it value-intializes (or default-constructs for structs/classes) the elements (so ints/floats/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







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?



Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory