none
Dynamic array in Visual Stdio IDE RRS feed

  • Question

  • Hello,

    We cannot run the below code in Visual Studio and I have an error but in other compiler it can be compile.

    What's the difference?

    #include<iostream>
    
    
    int main()
    {
        int n;
        cin>>n;
    
        int a[n];
    
        return 0 ;
    }
    

    Tuesday, December 3, 2019 6:48 AM

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
    Tuesday, December 3, 2019 7:24 AM
  • 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.


    Tuesday, December 3, 2019 7:44 AM

  • 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];
    
    

    As some additional background to this note the following.

    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
    Tuesday, December 3, 2019 9:26 AM

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
    Tuesday, December 3, 2019 7:24 AM
  • 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.


    Tuesday, December 3, 2019 7:44 AM

  • 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];
    
    

    As some additional background to this note the following.

    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
    Tuesday, December 3, 2019 9:26 AM