1

I know that int* array = new int [n]; allocates memory space in heap.

But if I declare something like that: int array[n]; in codeblocks it compile successfully, but if I move it to visual studio, it comes out of error.

What I want to ask is:

  1. What does int[n] really do and how does it compare to heap allocation? What is the difference between them?

  2. What the error may come out if I use int array[n] instead of int* array = new int [n];?

2
  • 3
    A variable length shouldn't be used for statically-sized arrays. Codeblocks accepts it because GCC probably has support for it as an extenstion. Commented Mar 9, 2013 at 18:20
  • 4
    Taken from GCC docs: Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. Commented Mar 9, 2013 at 18:20

4 Answers 4

4

int array[n] declares an array on the stack, not the heap. In standard C++ n must be a compile time constant, however some compilers have an extension that allows for variable length arrays declared in this form without a compile time constant n. It's not standard and probably shouldn't be used.

int array[n] can cause a couple problems over int* array = new int [n];:

  • If n is big enough you can cause a stack overflow. Or if the array is a class member and you create a large array of the class.
  • If array is a class member moving could be as expensive as copying
  • If array is a class member and you have given out pointers to the array and move the class, the pointers now refer to a post-moved object, or if random memory if the object was also destroyed. Either way, bad.
Sign up to request clarification or add additional context in comments.

5 Comments

So is there any potential problem come out when I use int array[n]?
Then why some compilers do the extensions for it? Thx
@Liang-YuPan Because it's in the C99 standard (C++ shares a lot in the C standard), and it's convenient. But it's not in the C++ standard
@Liang-YuPan Perhaps you should accept the answer if it helped.
3

GCC has an extension in C++ that allows int array[n] where n is not a compile time constant value. The standard language doesn't allow you to use non-constant value for n in such a case (it is allowed in C99).

Comments

3

If I understand your questions correctly, then I can answer both.

  1. int array[10] is allocated on the stack, whereas int *array = new int[10] is allocated on the heap. Usually, stack arrays only work with a constant number of elements, so int array[n] is not allowed in C90 (as long as n is not constant).

  2. MinGW (which is the version of the GCC compiler that codeblocks uses on windows) has an extension which does allow you to write int array[n] in C90 mode, where n is not constant. Visual C++ does not allow this.

Comments

2

when you write int array[n], that means you are performing a static allocation. i.e. memory will be allocated at the compile time. So using a variable length here will result in a compile time error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.