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 7664457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:18:39+00:00 2026-05-31T14:18:39+00:00

Possible Duplicate: C/C++: Array size at run time w/o dynamic allocation is allowed? In

  • 0

Possible Duplicate:
C/C++: Array size at run time w/o dynamic allocation is allowed?

In the following listing, clearly size of buf is determined by run-time constant j.
How does compiler generate code to allocate storage on stack(not knowing value of j at compile time)?

#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
  srandom(time(NULL));    
  int i = random();
  cout<< "random number: "<<i<<endl;
  if(i%2==0)
    i=2;
  else
    i=1;
  const int j=i;
  char buf[j];
  std::cout<<"size of buf array: "<<sizeof(buf)<<endl;
  return 0;   
}
  • 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-31T14:18:41+00:00Added an answer on May 31, 2026 at 2:18 pm

    I’m assuming that you are using gcc, and hence the VLA extension. This is not standard C++ and it was dropped from C++0x (1x).

    The reasoning is that it is not really all that useful and that a C++ implementation would be much more complex than one in C due to the stronger type system.

    Really, if you are stack allocating arrays of unknown size, you are at the same time writing dangerous code that may very well blow the stack. If you know the size then it is a non-issue, otherwise just dynamically allocate it. There are of course perfectly valid use cases and it is a “nice to have” feature, but they ultimately decided against it.

    Here is a good run down on the subject.

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

Sidebar

Related Questions

Possible Duplicate: Why can't I create an array with size determined by a global
Possible Duplicate: Is it possible to dynamically create an array of constant size in
Possible Duplicates: Malloc a 3-Dimensional array in C? dynamic allocation/deallocation of 2D & 3D
Possible Duplicate: Sizeof an array in the C programming language? Why is the size
Possible Duplicate: How does delete[] “know” the size of the operand array? Assume i
Possible Duplicate: Dynamic array in Stack? How do compilers treat variable length arrays Someone
Possible Duplicate: Array size too big - ruby Sorry if this has been asked,
Possible Duplicate: Use templates to get an array's size and end address Can someone
Possible Duplicate: How does this “size of array” template function work? Is there any
Possible Duplicate: How does delete[] “know” the size of the operand array? In the

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.