1

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?

Ravindra S
  • 6,302
  • 12
  • 70
  • 108

6 Answers6

6

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.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
1

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.

Jrud
  • 820
  • 3
  • 7
  • 21
  • I also agree with Keith Randall, even though it looks as though the statement I wrote redefines the size of the array the REASON it is inefficient is because behind the scenes it is really just making a second static array of the new size, copying the values into it, and then deleting the old one. – Jrud Nov 05 '09 at 04:48
0

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.

0

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.

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
  • Isn't it a dynamic array you are talking about? – Ravindra S Nov 05 '09 at 04:55
  • I wasn't sure entirely what you felt was useful about static arrays. If the useful part is the fact that you don't have worry about freeing them (and you couldn't care less about whether the language provides nice pretty syntax to create them), then alloca will do the trick. – Ken Bloom Nov 05 '09 at 05:04
  • 1
    @Ravi: no, not "dynamic" as in "dynamic lifetime", because arrays allocated by alloca() have automatic lifetime (i.e. same as local variables). However, their size is determined at runtime. – newacct Nov 05 '09 at 06:26
0

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.)

Justin Love
  • 4,397
  • 25
  • 36
0

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.