Discussion:
Advantages and disadvantages of static and dynamic arrays
(too old to reply)
j***@gmail.com
2017-05-22 13:52:45 UTC
Permalink
Hi,
Could someone tell me the advantages and disadvantages of using
static and dynamic arrays in C? Thank you in advance.
David
* Easy to allocate
* No need to free
* Fixed size at compile-time (< C99)
* Cannot be resized
* Maximum size
* No way to detect an allocation error
* Size can be determined at run-time
* Allocation errors can be detected
* Can be resized
* Limits are usually higher than automatic
* Program logic must explicitly allocate and deallocate data (less convenient)
Malcolm McLean
2017-05-22 14:12:35 UTC
Permalink
Hi,
Could someone tell me the advantages and disadvantages of using
static and dynamic arrays in C? Thank you in advance.
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.
s***@casperkitty.com
2017-05-22 14:26:57 UTC
Permalink
Post by Malcolm McLean
Hi,
Could someone tell me the advantages and disadvantages of using
static and dynamic arrays in C? Thank you in advance.
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.
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
2017-05-22 20:58:27 UTC
Permalink
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. 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
2017-05-22 21:43:03 UTC
Permalink
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.
Post by John Bode
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.
Post by John Bode
... 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.
Post by John Bode
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().
Post by John Bode
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.

Loading...