Sujet : Re: Two questions on arrays with size defined by variables
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 10. Feb 2025, 07:49:10
Autres entêtes
Organisation : None to speak of
Message-ID : <87cyfqpa55.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8
User-Agent : Gnus/5.13 (Gnus v5.13)
Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> writes:
On 10.02.2025 03:12, Janis Papanagnou wrote:
[...]
The Cim compiler I use actually supports a constant initialization -
but I'm positive this is non-standard! - like
begin
integer n = 10;
integer array arr (1:n);
arr(5) := 9;
end
>
I should have noted that this (non-standard) syntax doesn't really
address the problem (since you have just a named constant that you
may use in several places, but the array is *not* really dynamic,
its memory demands are fixed and known for that block).
>
I think the primary point for an explanation of the necessity of a
block structure is different. - It is typical that stack memory is
allocated when a block is "entered". For the stack allocation the
size of all local objects should be known. But in the first example
above the outer block has only knowledge about the size of the 'n'
integer variable, but without a concrete value for 'n' we don't
know how many space the dynamic array will need. But if we open a
new (subordinated) block the array size is determined if declared
there.
In C lifetime of a non-VLA object with automatic storage duration
(defined within a block without "static") extends from when execution
reaches the opening "{" of the enclosing block to when it reaches the
closing "}". There are well-defined ways you can even access such an
object in code that appears textually before its definition (by saving
its address and using a goto). This means that a conforming
implementation *must* allocate memory for non-VLA automatic objects on
block entry; waiting until the definition is encountered would be
non-conforming.
But a VLA object's lifetime begins, not when execution reaches the
opening "{", but when it reaches the object definition.
[...]
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */