简体   繁体   中英

Can I increase the size of a statically allocated array?

I know its possible to increase the size of a dynamically allocated array.

But can I increase the size of a statically allocated array? If yes,how?

EDIT: Though this question is intended for C language, consider other languages too.Is it possible in any other language?

Simple answer is no, this cannot be done. Hence the name "static".

Now, lots of languages have things that look like statically allocated arrays but are actually statically allocated references to a dynamically allocated array. Those you could resize.

in VB .NET it would be:

Redim Preserve ArrayName(NewSize)

not sure what langauge you're after though...

And I wouldn't use this command a lot... its terribly inefficient. Linked lists and growing data structures are much more efficient.

No. It is not. There are two options here:

  1. Use a dynamic one
  2. Or,at the risk of wasting memory, if you have an idea about the maximum number of elements that the array will store, statically allocate accordingly

Yes, that was C.

If you're careful, you can use alloca() . The array is allocated on the stack, but in terms of the code style it's a lot like if you used malloc (you don't have to free it though, that's done automatically). I'll let you decide whether to call that a "static" array.

No. Static allocation gives the compiler permission to make all kinds of assumptions which are then baked into the program during compilation.

Among those assumptions are that:

  1. it is safe to put other data immediately after the array (not leaving you room to grow), and
  2. that the array starts at a certain address, which then becomes part of the machine code of the program; you can't allocate a new array somewhere (and use it) because the references to the address can't be updated.

(Well, references could be updated, if the program was stored in ram, but self-modifying programs are highly frowned upon, and surely more trouble than dynamic arrays.)

Technically, in C it isn´t even possible to increase the size of a dynamically allocated array.

In fact, realloc() does some kind of "create new object & copy the data" routine. It does not modify the size of an existant heap-memory object at all.

So the answer is simple as that, that you are not be able to change the size of any object or array of objects after it has been allocated, neither if it was dynamically or statically allocated.

What you can do is to use the same strategy by developing a function which is creating another static allocated array of objects with the desired size and copy the data. If the new array of objects is smaller than the old one, the values inside the difference are discarded.

The only difference is, that the size of the new array, equivalent to the size of the old array, need to be fixed at compile-time.

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