3

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;
    }
}
Qiang Li
  • 10,593
  • 21
  • 77
  • 148
  • They're different because Java is a different language. – Mooing Duck Nov 27 '11 at 00:57
  • Of course they are different languages! :) Can you explain besides that basic `difference`, what else is `different`? – Qiang Li Nov 27 '11 at 00:59
  • QiangLi: Pretty much everything besides the syntax is different. And a fair amount of the syntax as well. Java and C++ are about as similar as English and Latin. – Mooing Duck Nov 27 '11 at 01:37
  • @MooingDuck: English and Latin? Really? I've never studied Java, and yet I can read it near fluently due to my training in C++. Hardly the same can be said for a native English speaker trying to read Latin. – Benjamin Lindley Nov 27 '11 at 01:43
  • @MooingDuck: That does not say anything. I want to just focus on this point: using a local array whose length is based on function arguments. Can you do this in C++ and Java, and why or why not? – Qiang Li Nov 27 '11 at 01:45

3 Answers3

7

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.

Community
  • 1
  • 1
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • Thanks! In your link to VLA, it says Java does not provide VLA. But my trial was on the contrary. Do you have some more to say? Thanks again! – Qiang Li Nov 27 '11 at 00:58
  • @QiangLi: Java does not support VLA as C99 does. There is no way in Java to create array on stack. I am not even sure if you can create anything on stack there except unboxed POD types... –  Nov 27 '11 at 01:00
  • If you scroll down to the bottom of that wiki page, it explains the situation for Java very clearly. In Java, arrays are objects which are on the heap rather than on the stack. – Mysticial Nov 27 '11 at 01:00
  • @Mysticial, can you see to my attached Java code as to why it is compiling? – Qiang Li Nov 27 '11 at 01:08
  • Technically, that's not a VLA because `new` allocates a new object on the heap. – Mysticial Nov 27 '11 at 01:11
  • "VLA does not exist", as you said. But what would you call `c` in Java? Isn't its length also variable? :) – Qiang Li Nov 27 '11 at 01:15
  • I think you're mixing up the concept of heap vs. stack memory. A VLA is defined as being in "automatic storage" (the stack). An array on the heap is *not* a VLA. In Java, all arrays are on the heap. None of them are on the stack. Therefore VLAs don't exist in Java. However, in C/C++, arrays can be either on the stack or on the heap. In your example, that array you created of size `n` is created on the heap. – Mysticial Nov 27 '11 at 01:18
  • OK, in terms terminology, java does not have VLAs. But in terms of functionality availability, java does allow me to use arrays with variable length, while c++ does not, unless some compiler has such extension as g++ does. Is this understanding correct? – Qiang Li Nov 27 '11 at 01:31
  • 1
    Yes, that is correct. In standard C++, you will need to allocate to create arrays of variable length. – Mysticial Nov 27 '11 at 01:33
  • @Mysticial To be exact Java doesn't specify where the arrays are allocated. Hence the JIT is allowed to do EA and allocate objects (including arrays) on the stack. Not that surprising that Java doesn't make a distinction there. But really, VLAs are a bad, bad idea and I'm more than happy that C++ kept at least that one out of the language. – Voo Nov 27 '11 at 02:16
6

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

Martin B
  • 23,670
  • 6
  • 53
  • 72
  • 1
    This is ISO C99 variable length array, and not an extension. GCC accepts them in C90 and C++ as an extension. –  Nov 27 '11 at 00:58
2

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[].

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249