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.