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

  • Home
  • SEARCH
  • 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 6117653
In Process

The Archive Base Latest Questions

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

GCC gives no error when you initialize a variable-sized array as long as the

  • 0

GCC gives no error when you initialize a variable-sized array as long as the variable is const, but when it isn’t, it won’t compile.

What’s the reason behind this? What’s so wrong with doing:

int size = 7;
int test[size] = {3, 4, 5};

That won’t compile at all, but if I don’t initialize test[] then it does compile! That doesn’t make any sense to me, because as far as I know a stack frame needs to be made to fit this array according to its size(7 ints) no matter what(which means the integer literals I use don’t really have any meaning, if I’m not mistaken), so what difference does it make if I initialize it or not?

Just another one of my crazy C++ design questions…

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-23T15:20:44+00:00Added an answer on May 23, 2026 at 3:20 pm
    • The size of the array must be a constant integral expression.
    • An integral literal is a constant integral expression. (int arr[5];)
    • A constant integral variable initialized with a constant expression is a constant expression. (const int j = 4; const int i = j; int a[i];)

    • A constant variable initialized with a non-constant expression is not a constant expression

       int x = 4;  // x isn't constant expression because it is not const
       const int y = x; //therefore y is not either
       int arr[y]; //error)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to compile a file that instantiates this class. GCC gives me cryptic
What's wrong with the following? typedef boost::shared_ptr SharedPtr; GCC gives the following error: ISO
The code given here when compiled by g++ runs fine but gives error on
Following code does compile in gcc 4.5 but it is not being compiled in
I'm using GCC to generate a dependency file, but my build rules put the
why the below code gives me error of double free or corruption... when i
I have a C code in long file that is compiled using cc. But
Can anybody please help me make this work? Donor-enumerate() doesn't work, gcc gives no
I am trying to write a trigger but getting syntax error: CREATE TRIGGER archive_domain_trig
I have a variable k of type const char * , and a function

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.