7044

Is declaring a local array based on function arguments legal in c++

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.

<strong>Edit</strong>

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.

<strong>Edit 2</strong>

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; } }

Answer1:

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.

<strong>EDIT :</strong>

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?

<strong>EDIT 2:</strong>

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.

Answer2:

This is a GCC extension -- most other C++ compilers do not allow this.

Answer3:

As others have mentioned, those are called Variable Length Arrays in C99, but in C++ they are not allowed except by way of GCC extension, which you shouldn't use if you want your code to be portable.

In C++, to use an array (-like object) whose size you can't know until runtime, use std::vector or new[]/delete[].

Recommend

  • Visual Studio 2015 Using Incorrect Cordova Version
  • Search feature with multiple criteria - PHP/MySQL
  • Julia custom type assignment
  • Is it better to have a method's datatype be as specific as possible or more general?
  • Neo4J / py2neo — cursor-based query?
  • Why are the debugger memory windows not available in VS 2015?
  • Meteor: Block access to application if user's email is not verified
  • Authentication - JavaScript - Logout issue
  • Base Internationalization and “Could not find a storyboard named […]”
  • UITableView takes much longer to load when numberOfRows returns a large number
  • phpmailer - How to verify a sent email arrived at its destination
  • Return null in boolean to checkbox state converter in XAML
  • Azure table query partial partitionkey guid match
  • Using an STL Iterator without initialising it
  • Getting unread count in Sent Folder using Google Apps Script - GMail
  • SIP API media codecs
  • Does Apportable support to build library binary (.a/.so)?
  • hide missing dates from x-axis ggplot2
  • Web.config system.webserver errors
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to create a file in java without a extension
  • Meteor: Do Something On Email Verification Confirmation
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Cross-Platform Protobuf Serialization
  • Where to put my custom functions in Wordpress?
  • Javascript + PHP Encryption with pidCrypt
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • RestKit - RKRequestDelegate does not exist
  • How to format a variable of double type
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • coudnt use logback because of log4j
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy