Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Advantages and disadvantages of static and dynamic arrays

4,981 views
Skip to first unread message

cyberdude

unread,
May 22, 2003, 5:59:36 AM5/22/03
to
Hi,

Could someone tell me the advantages and disadvantages of using
static and dynamic arrays in C? Thank you in advance.

David

Siddharth Kashyap

unread,
May 22, 2003, 9:01:09 AM5/22/03
to
Hi,

Dynamic Arrays:

You use them when you don't know at compile time the size of the array.
You only get the size requirement at run time.
It is the programmers responsibility to free up the memory allocated for
these arrays. Otherwise, we can have memory leak.
Also, since the memory is allocated at runtime, it takes more time.


Static Arrays:
You use them when you know at compile time the size of the array.
Size of this will not change during the execution of the program.
Memory requirements works just as same for any other variable.
Programmer does not have to request, or free any memory.
Since most of the work is done at run time, the program is faster.


Siddharth.


"cyberdude" <quake_ea...@hotmail.com> wrote in message
news:bai728$lkm$1...@news.ust.hk...

Eric Sosman

unread,
May 22, 2003, 10:35:07 AM5/22/03
to
Siddharth Kashyap top-posted [here rearranged]:

> "cyberdude" <quake_ea...@hotmail.com> wrote:
> >
> > Could someone tell me the advantages and disadvantages of using
> > static and dynamic arrays in C? Thank you in advance.
>
> Dynamic Arrays:
>
> You use them when you don't know at compile time the size of the array.
> You only get the size requirement at run time.
> It is the programmers responsibility to free up the memory allocated for
> these arrays. Otherwise, we can have memory leak.
> Also, since the memory is allocated at runtime, it takes more time.
>
> Static Arrays:
> You use them when you know at compile time the size of the array.
> Size of this will not change during the execution of the program.
> Memory requirements works just as same for any other variable.
> Programmer does not have to request, or free any memory.
> Since most of the work is done at run time, the program is faster.

Another difference is that a static array has a lifetime
equal to that of the program as a whole, while a dynamic
array's lifetime can be controlled by the programmer.

--
Eric....@sun.com

bd

unread,
May 22, 2003, 12:54:16 PM5/22/03
to
cyberdude <quake_ea...@hotmail.com> wrote in message news:<bai728$lkm$1...@news.ust.hk>...

Automatic (not static):
Advantages:
* Easy to allocate
* No need to free
Disadvantages:
* Fixed size at compile-time (< C99)
* Cannot be resized
* Maximum size
* No way to detect an allocation error

Dynamic:
Advantages:
* Size can be determined at run-time
* Allocation errors can be detected
* Can be resized
* Limits are usually higher than automatic
Disadvantages:
* Program logic must explicitly allocate and deallocate data (less convenient)

jado...@gmail.com

unread,
May 22, 2017, 9:52:54 AM5/22/17
to

Malcolm McLean

unread,
May 22, 2017, 10:12:43 AM5/22/17
to
You've actually got three, not two options

Option 1, a static array in global and or file scope.
Option 2, an array on the stack, as a local variable
Option 3, a dynamic array

Option 1 ties up memory for the entire duration of the program. Which
means you can't have a memory allocation failure as long as the program
as a whole loads and runs. The data is accessible to all functions in
scope. It's also the method of choice if you are initialising the array
wth a lot of compile time data, e.g. a hardcoded image.

Option 2 means that the array will be created when the function holding it
is called. It's then usual to pass down pointer to subroutines. In C89
(still in common use), you need to know the maximum size of the array
at compile time. If the array is very large, you can have a stack overflow
error which will become apparent at runtime.

Option 3 means that you must call malloc(), which incurs an overhead.
The array can be of any size, and on a modern desktop system the amount
of memory available to malloc will be of the order of a gigabyte whilst
the amount available on the stack will likely only be a few megabytes.
Also, the data is less likely to be kept in the cache than for a small array on
the stack. You have to free the data after using it, which is a burden.

supe...@casperkitty.com

unread,
May 22, 2017, 10:27:05 AM5/22/17
to
On some systems, static storage may be further subdivided into static const,
static initialized, and static zero-filled. Static const and zero-filled may
occupy physically different regions (ROM and RAM) with independent size
limitations, while initialized data may occupy space in both. Depending upon
the implementation, a declaration like "uint16_t arr[1000] = {1,2,3};" may
store all 4000 bytes of arr's initial value in ROM, or it may result in arr
being zero-filled and then having three values stored into it.

Additionally, some implementations may have separate limits for static and
allocated storage. Segmented 8086 compilers may limit the total size of
all static allocations to 64K, and classic Macintosh compilers often limited
it to 32K, but both implementations could access much more storage on the
heap. Especially when programming for such systems, it was common to use
malloc() to acquire storage that would never be freed. Even on systems
without such limitations, such an approach may be helpful if the necessary
size of an array won't be known until runtime. Storage created with malloc()
should free storage when it is no longer needed, but if an array will be used
for the entire lifetime of a program it may be allocated and then treated much
like static storage from then on.

John Bode

unread,
May 22, 2017, 4:58:46 PM5/22/17
to
Arrays with "static" storage duration (declared outside of any block or
function or with the "static" keyword) have lifetimes that extend over
the lifetime of the entire program. Any such array declared within a
function or block will retain its value between function calls; however,
that function is no longer re-entrant. Objects with static storage
duration are implicitly initialized to 0 or NULL. Such arrays can be
very large, but their size must be specified at compile time and they
cannot be resized after they are defined.

Arrays with "automatic" storage duration (declared within a function or
block without the "static" keyword) have lifetimes that extend over their
enclosing block. Variable-length arrays have their sizes specified at
run time vs. compile time, but once defined, they cannot be resized. Such
arrays may be limited in size (I can declare an auto array of about
8.2 MB on my system at work, but larger than that I get a segfault at
runtime).

Arrays with "allocated" storage duration (i.e., dynamic arrays) have
lifetimes that extend from the initial "malloc/calloc/realloc" call until
a "free" call (which may be in a different function from the "*alloc" call).
Such arrays can be resized as necessary with calls to "realloc". You are
100% responsible for managing this memory properly - there's no automatic
garbage collector that cleans up after you if there are no more references
to that memory. There's no fixed size limit on dynamic buffers - it depends
on how fragmented your heap is.

James R. Kuyper

unread,
May 22, 2017, 5:43:11 PM5/22/17
to
I decided to expand on your description with regards to how
initialization depends upon the storage duration. There's nothing wrong
with your description, other than being less complete than mine.

On 05/22/2017 04:58 PM, John Bode wrote:
> On Thursday, May 22, 2003 at 4:59:36 AM UTC-5, cyberdude wrote:
>> Hi,
>>
>> Could someone tell me the advantages and disadvantages of using
>> static and dynamic arrays in C? Thank you in advance.
>>
>> David
>
> Arrays with "static" storage duration (declared outside of any block or
> function or with the "static" keyword) have lifetimes that extend over
> the lifetime of the entire program. Any such array declared within a
> function or block will retain its value between function calls; however,
> that function is no longer re-entrant. Objects with static storage
> duration are implicitly initialized to 0 or NULL ...

, depending upon whether they have arithmetic or pointer type. Note that
this applies recursively to the elements of an array and to the members
of a struct, and to the first member of an object of union type, and
that it applies only if the object is not explicitly initialized to some
other value.

> ... Such arrays can be
> very large, but their size must be specified at compile time and they
> cannot be resized after they are defined.
>
> Arrays with "automatic" storage duration (declared within a function or
> block without the "static" keyword) have lifetimes that extend over their
> enclosing block. Variable-length arrays have their sizes specified at
> run time vs. compile time, but once defined, they cannot be resized. Such
> arrays may be limited in size (I can declare an auto array of about
> 8.2 MB on my system at work, but larger than that I get a segfault at
> runtime).

Objects with automatic storage duration are uninitialized unless
explicitly initialized.

For both static and automatic storage durations, if only part of an
object with an aggregate (array or struct) type is explicitly
initialized, the other parts are implicitly initialized to zero or null.

> Arrays with "allocated" storage duration (i.e., dynamic arrays) have
> lifetimes that extend from the initial "malloc/calloc/realloc" call until
> a "free" call (which may be in a different function from the "*alloc" call).

If allocated by a call to malloc(), the memory is uninitialized. If
allocated using calloc(), each byte is zero-initialized (which, for
pointers, need not result in a null pointer, which is a difference from
static and automatic arrays). If realloc() needs to allocate new memory,
it behaves the same as malloc(), not calloc().

> Such arrays can be resized as necessary with calls to "realloc". You are
> 100% responsible for managing this memory properly - there's no automatic
> garbage collector that cleans up after you if there are no more references
> to that memory. There's no fixed size limit on dynamic buffers - it depends
> on how fragmented your heap is.

The original message is 14 years old, and at that time those were the
only three storage durations. However, since C2011 came out, there's a
fourth storage duration: an array with thread storage duration is very
similar to an array with static storage duration, except that there is a
separate object with that name for each thread, and each such object has
a lifetime that is the same as the lifetime of the corresponding thread,
rather than the lifetime of the entire program.
0 new messages