Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:20:43+00:00 2026-05-31T07:20:43+00:00

The following code compiles and it seems to run fine: class Test { private:

  • 0

The following code compiles and it seems to run fine:

class Test {
  private:
     const unsigned MAX;

  public:

     Test (const unsigned int n) : MAX(n) { }

     void foo () {
         int array[MAX];
         ...
     }

};

but is it really OK? I mean:

Test a (3);
Test b (8);

does array actually have 3 and 8 cells respectively?

If so, is it because array is an automatic var and gets instantiated with the appropriate dimension?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:20:44+00:00Added an answer on May 31, 2026 at 7:20 am

    What you have written is valid in c99 but not valid c++.

    I am of course talking about your use of VLA‘s, not the full snippet.


    When compiling using g++ -pedantic -ansi -Wall we get the below warning;

    foo.cpp: In member function 'void Test::foo()':
    foo.cpp:18:23: warning: ISO C++ forbids variable length array 'array' [-Wvla]
    

    As mentioned in the above warning the pattern you are using is often referred to as using a variable length array, which is standard in C99 and “allowed” in C++ through a g++ extension.

    I’d recommend you to use a STL container instead of hacks as these, for one single reason; what you are doing is not legal, and therefor isn’t guaranteed to be portable cross compilers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I compile the following code with g++, the object of class A seems
The following source code compiles correctly with Visual Studio 2010: namespace NS { class
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
Why won't the following C code compile? It seems like it should just change
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
the following code compiles with Visual Studio 2008 but not with g++ on Mac
How is it possible that following code even compiles? As far as I can
Visual Studio 2008 I am using the following source code that compiles ok using
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.