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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:27:38+00:00 2026-05-25T21:27:38+00:00

This might seem like a beginner’s question, but I am interested in the way

  • 0

This might seem like a beginner’s question, but I am interested in the way that a compiler normally creates arrays of variable-dimensions, like in the following program.

#include<iostream>

int main(){
  int n;
  std::cin>>n;
  int a[n];
}

From what I’ve learnt, in C all the initializer values must be constant, so that the compiler knows how much memory to reserve inside the function, normally by subtracting the stack pointer in order to accomodate the number of elements the array holds.

This makes sense to me.
However, I don’t quite understand how compilers treat the above program, since it seems to work with G++(MinGW) , but fails with Cl, Microsoft’s C++ compiler. I suspect that GCC allocates memory on the heap trough a non-standard extension, but I am not sure of this.

Also, Microsoft’s compiler is not renowned for being standards-compliant, so I wouldn’t be surprised if it may actually be wrong in the way it treats the above program.

  • 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-25T21:27:39+00:00Added an answer on May 25, 2026 at 9:27 pm

    In the C99 version of the C standard, variable length arrays are permitted. However, they are not permitted in any version of C++; you’re seeing a G++ extension. Note that Microsoft’s C compiler does not fully support C99; since G++ supports C99 it’s easy enough to apply the VLA support to C++ as an extension.

    As to how the compiler usually implements VLAs, it’s the same as alloca() (except that it has to keep the size around for sizeof) – the compiler saves the original stack pointer, then adjusts it down by however many bytes it calculates that it needs. The downside is function entry and exit is a bit more complicated as the compiler needs to store where to reset the stack pointer to rather than just adjusting by fixed constants.

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

Sidebar

Related Questions

This might seem like a very easy question for some of you folks, but
This might seem like a weird question, but bear with me. I'd like to
This might seem like a trivial question, but I'm a bit muddled in my
This might sound like an obvious question but I can't seem to find the
This might seem like a silly question but valgrind doesn't by default give you
This might seem like a really dumb question, but I am writing an application
This might seem like a stupid question but it's been a long day. I'm
This might seem a bit like a do-my-homework-for-me question (and it is), but I
This might seem like a silly question, but after asking some questions on stackoverflow
This might seem like an unordinary json question but I can assure you I

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.