简体   繁体   中英

Why is new int[n] valid when int array[n] is not?

For the following code:

foo(int n){
    int array[n];
}

I understand that this is invalid syntax and that it is invalid because the c++ standard requires array size to be set at compile time (although some compilers support the following syntax).

However I also understand the the following is valid syntax:

bar(int n){
    int *array = new int[n];
}

I don't understand why this is allowed, isn't it the same as creating an array where the size is determined at runtime? Is it good practice to do this or should I be using a vector if I need to do this instead?

That's because the former is allocated on the stack and the latter on the heap.

When you allocate something on the stack, knowing the size of the object is essential for correctly building it. C99 allows the size to be specified at run time, and this introduces some complications in building and dismantling the aforementioned stack, since you cannot calculate its size at compile time. Machine code must be emitted in order to perform said calculation during the execution of the program. This is probably the main reason why this feature wasn't included in the C++ standard.²

On the contrary, the heap has no fixed structure, as the name implies. Blocks of any size can be allocated with no particular order, as long as they do not overlap and you have enough (virtual) memory¹. In this case, knowing the size at compile time is not that relevant.

Also, remember that the stack has a limited size, mostly to detect infinite recursions before they consume all the available memory. Usually the limit is fixed around 1MB, and you rarely reach that. Unless you allocate large objects, which should be placed in the heap.

As of what you should use, probably a std::vector<int> . But it really depends on what you are trying to do.

Also note that C++11 has a std::array class, whose size must be known at compile time. C++14 should have introduced std::dynarray , but it was postponed because there is still much work to do concerning compile-time unknown size stack allocation.


¹ blocks are usually allocated sequentially for performance reasons, but that's not required.

² as pointed out, knowing the size at compile time is not a hard requirement, but it makes things simpler.

In the first case you are allocating the memory space statically to hold the integers. This is done when the program is compiled and so the amount of storage is inflexible.

In the latter case you are dynamically allocating a memory space to hold the integers. This is done when the program is run, and so the amount of storage required can be flexible.

The second call is actually a function that talks to the operating system to go and find a place in memory to use. That same process does not happen in the first case.

int array[n] allocates a fixed-length array on the call stack at compile-time, and thus n needs to be known at compile-time (unless a compiler-specific extension is used to allow the allocation at runtime, but the array is still on the stack).

int *array = new int[n] allocates a dynamic-length array on the heap at run-time, so n does not need to be known at compile-time.

The one and only valid answer to your question is, because the standard says so .

In contrast to C99, C++ never bothered to specify variable length arrays (VLAs), so the only way to get variably sized arrays is using dynamic allocation, with malloc , new or some other memory-manager.

In fairness to C++, having runtime-sized stack-allocations slightly complicates stack-unwinding, which would also make exception-handling for the functions using the feature consequently more bothersome.

Anyway, even if your compiler provides that C99-feature as an extension, it's a good idea to always keep a really tight rein on your stack-usage:
There is no way to recover from blowing the stack-limit, and the error-case is simply left Undefined Behavior for a reason.

The easiest way to simulate VLAs in C++, though without the performance-benefit of avoiding dynamic allocation (and the danger of blowing the limit):

unique_ptr<T[]> array{new T[n]};

In the expression

new int[n]

int[n] is not the type. C++ treats " new with arrays" and " new with non-arrays" differently. The N3337 standard draft has this to say about new :

When the allocated object is an array (that is, the noptr-new-declarator syntax is used or the new-type-id or type-id denotes an array type), the new-expression yields a pointer to the initial element (if any) of the array.

The noptr-new-declarator refers to this special case (evaluate n and create the array of this size), see:

noptr-new-declarator :

[ expression ] attribute-specifier-seq opt

noptr-new-declarator [ constant-expression ] attribute-specifier-seq opt

However you can't use this in the "usual" declarations like

int array[n];

or in the typedef

typedef int variable_array[n];

This is different with C99 VLAs, where both are allowed.

Should I be using vectors instead?

Yes, you should. You should use vectors all the time, unless you have a very strong reason to do otherwise (there was one time during the last 7 years when I used new - when I was implementing vector for a school assignment).

This is because the C++ language does not have the C feature introduced in C99 known as "variable length arrays" (VLA).

C++ is lagging in adopting this C feature because the std::vector type from its library fulfills most of the requirements.

Furthermore, the 2011 standard of C backpedaled and made VLA's an optional feature.

VLA's, in a nutshell, allow you to use a run-time value to determine the size of a local array that is allocated in automatic storage:

int func(int variable)
{
   long array[variable]; // VLA feature

   // loop over array

   for (size_t i = 0; i < sizeof array / sizeof array[0]; i++) {
     // the above sizeof is also a VLA feature: it yields the dynamic
     // size of the array, and so is not a compile-time constant,
     // unlike other uses of sizeof!
   } 
}

VLA's existed in the GNU C dialect long before C99. In dialects of C without VLA's, array dimensions in a declaration must be constant expressions.

Even in dialects of C with VLA's, only certain arrays can be VLA's. For instance static arrays cannot be, and neither can dynamic arrays (for instance arrays inside a structure, even if instances of that structure are allocated dynamically).

In any case, since you're coding in C++, this is moot!

Note that storage allocated with operator new are not the VLA feature. This is a special C++ syntax for dynamic allocation, which returns a pointer type, as you know:

int *p = new int[variable];

Unlike a VLA's, this object will persist until it is explicitly destroyed with delete [] , and can be returned from the surrounding scope.

No, the second is not declaring an array. It's using the array form of operator new , and that specifically permits the first dimension to be variable.

Because it has different semantics:

If n is a compile-time constant (unlike in your example):

int array[n]; //valid iff n is compile-time constant, space known at compile-time

But consider when n is a runtime value:

int array[n]; //Cannot have a static array with a runtime value in C++
int * array = new int[n]; //This works because it happens at run-time, 
                          // not at compile-time! Different semantics, similar syntax.

In C99 you can have a runtime n for an array and space will be made in the stack at runtime. There are some proposals for similar extensions in C++, but none of them is into the standard yet.

You can allocate memory statically on the stack or dynamically on the heap .

In your first case, your function contains a declaration of an array with a possible variable length, but this is not possible, since raw arrays must have fixed size at compile time , because they are allocated on the stack . For this reason their size must be specified as a constant, for example 5 . You could have something like this:

foo(){
    int array[5]; // raw array with fixed size 5
}

Using pointers you can specify a variable size for the memory that will be pointed, since this memory will be allocated dynamically on the heap. In your second case, you are using the parameter n to specify the space of memory that will be allocated.

Concluding, we can say that pointers are not arrays: the memory allocated using a pointer is allocated on the heap , whereas the memory allocated for a raw array is allocated on the stack .

There are good alternatives to raw arrays , for example the standard container vector , which is basically a container with variable length size.

Make sure you understand well the difference between dynamic and static memory allocation, the difference between memory allocated on the stack and memory allocated on the heap .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM