Answered by:
Dynamic array in Visual Stdio IDE

Question
-
Answers
-
Hello,
arrays must be defined with a const value. Your n is not const. When the programme comes to an array definition, it allocates memory on the stack. The compiler must know who much memory it must reserve.
If your code works on other compilers, then it is a special case in the compiler. But as far as I know, it's not Standard.
Dynamic Arrays (that will normally be reserved on the heap) must be defined with new and delete.
int main() { int n; cout << "n="; cin>>n; if (n <= 0) { cout << "n must be positive."; return 1; } // allocate dynamic memory int *a = new int[n]; if (!a) { cout << "Could not allocate Memory."; return 1; } //… use a // free memory delete[] a; return 0 ; }
https://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/
http://www.cplusplus.com/doc/tutorial/dynamic/
Regards, Guido
- Marked as answer by Arash_89 Tuesday, December 3, 2019 9:04 PM
-
Hello,
Thank you for posting here.
This is due to the standard. Visual Studio supports C89, and the compiler you posted supports C99. It enhances the array in C99, which supports use variable as the length of array.
FYI. https://docs.microsoft.com/en-us/cpp/c-language/c-language-reference?view=vs-2019
Best Regards,
Suarez Zhou
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Suarez-ZhouMicrosoft contingent staff Tuesday, December 3, 2019 7:45 AM
- Marked as answer by Arash_89 Tuesday, December 3, 2019 9:06 PM
-
We cannot run the below code in Visual Studio and I have an error but in other compiler it can be compile.
int n; cin>>n; int a[n];
Variable Length Arrays (VLAs) were introduced into the C language Standard with
version C99. In the later version of that Standard C11 (2011) they were made optional.
N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
6.7.6.2 Array declarators
"4. ... (Variable length arrays are a conditional feature that implementations
need not support; see 6.10.8.3.)"
Note that these comments refer to the C language Standard, and you are using C++.
The Standard for the C++ language incorporates the C Standard. So in C++ as well
as C VLAs are optional, and MS decided long ago that they would not implement
them for either language.
- Wayne
- Marked as answer by Arash_89 Tuesday, December 3, 2019 9:07 PM
All replies
-
Hello,
arrays must be defined with a const value. Your n is not const. When the programme comes to an array definition, it allocates memory on the stack. The compiler must know who much memory it must reserve.
If your code works on other compilers, then it is a special case in the compiler. But as far as I know, it's not Standard.
Dynamic Arrays (that will normally be reserved on the heap) must be defined with new and delete.
int main() { int n; cout << "n="; cin>>n; if (n <= 0) { cout << "n must be positive."; return 1; } // allocate dynamic memory int *a = new int[n]; if (!a) { cout << "Could not allocate Memory."; return 1; } //… use a // free memory delete[] a; return 0 ; }
https://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/
http://www.cplusplus.com/doc/tutorial/dynamic/
Regards, Guido
- Marked as answer by Arash_89 Tuesday, December 3, 2019 9:04 PM
-
Hello,
Thank you for posting here.
This is due to the standard. Visual Studio supports C89, and the compiler you posted supports C99. It enhances the array in C99, which supports use variable as the length of array.
FYI. https://docs.microsoft.com/en-us/cpp/c-language/c-language-reference?view=vs-2019
Best Regards,
Suarez Zhou
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Suarez-ZhouMicrosoft contingent staff Tuesday, December 3, 2019 7:45 AM
- Marked as answer by Arash_89 Tuesday, December 3, 2019 9:06 PM
-
We cannot run the below code in Visual Studio and I have an error but in other compiler it can be compile.
int n; cin>>n; int a[n];
Variable Length Arrays (VLAs) were introduced into the C language Standard with
version C99. In the later version of that Standard C11 (2011) they were made optional.
N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
6.7.6.2 Array declarators
"4. ... (Variable length arrays are a conditional feature that implementations
need not support; see 6.10.8.3.)"
Note that these comments refer to the C language Standard, and you are using C++.
The Standard for the C++ language incorporates the C Standard. So in C++ as well
as C VLAs are optional, and MS decided long ago that they would not implement
them for either language.
- Wayne
- Marked as answer by Arash_89 Tuesday, December 3, 2019 9:07 PM