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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:29:39+00:00 2026-05-27T07:29:39+00:00

I read from a book saying that the following c++ code should not compile:

  • 0

I read from a book saying that the following c++ code should not compile:

  void f(int n, int m){
     int a[n] , b[n][m];
  }

because the size of the arrays are not determined at compile time.

But I tried it out and found no matter the function is a global one or a member function, I could get compilation successful using g++.

Was this something made legal in recent c++ implementation, or the book is simply wrong.

Thank you.

Edit

I saw a few replies immediately. I just have this wonder too in Java. I notice in java, this is supported (please correct me if this is also version dependent). So why the difference? Does it have anything to do with using references vs. objects? But still, in java, I can declare an array with variable length from function argument for primitives.

Edit 2

The following Java code did compile though, if you say it should not:

class Test1 {
    public int[] f(int n,int k){
        int[] c=new int[n];
        Arrays.fill(c, k);
        return c;
    }
}
  • 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-27T07:29:40+00:00Added an answer on May 27, 2026 at 7:29 am

    These are called Variable Length Arrays. They are not allowed in C++. But some compilers (such as GCC) support them as an extension.

    In C99, Variable Length Arrays are allowed.

    EDIT :

    For your new question. The top answer for this question explains why C++ does not have variable length arrays.

    Why no variable size array in stack?

    EDIT 2:

    In Java, arrays are objects which are stored on the heap rather than the call stack. Therefore the question is moot – all arrays are on the heap, hence VLAs don’t exist.

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

Sidebar

Related Questions

I read code from one book and have this method: public int getScore(String name)
History: I read from one of Knuth's algorithm book that first computers used the
I read from a book about tentative defination that, A tentative definition is any
I read this code from my book: class B extends A {...} class G<E>
I read from some books that Phusion Passenger is the answer to easy Ruby
From what I've read from Herb Sutter and others you would think that volatile
i read this statement from a book i read The Document Object Model or
I wrote this simple test code, by adapting a piece from a book, to
I need help remembering what book I read this from, It might have been
I'm trying to learn programming by myself, I'm working from a book that has

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.