Re: automatic variables are not initialized correctly?
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Sat, 17 Jun 2006 22:37:36 -0700
Ron Shepard wrote:
(snip)
This kind of semantics is useful, so why should it be disallowed? A local variable is simply one that has scope (i.e. can be accessed) only within the subprogram. SAVE means that its value is retained from one call to the next. These are not inconsistent ideas, it is quite useful to be able to do this, and it has been a standard part of the language since 1978.
PL/I uses INTERNAL to describe local variables (visible only within that scoping unit, and EXTERNAL to describe global variables.
Then STATIC, AUTOMATIC, and CONTROLLED to describe the allocation
method, where CONTROLLED is similar to Fortran ALLOCATABLE.
The combination INTERNAL AUTOMATOC isn't allowed, but the rest are.
(Including CONTROLLED EXTERNAL, which would be somewhat like putting
Fortran ALLOCATABLE in COMMON. I forget if that is allowed.)
The new part, the part that I'm still not sure I agree with, is the idea of making local variables SAVEd, implicitly, simply because there is an initialization expression. F77 did not have initialization statements (it used DATA instead), so it was less confusing there.
Well, DATA is a non-executable statement. Static allocation was
popular, if not required by the standard. Initializing a variable
and then testing it was often used by programs that needed an
initialization. Random number subprograms would use static
data to store the random seed for next time.
If you just look at an initialization statement, out of context, then it is not clear whether that initialization is done once with an implied SAVE, or whether it is done repeatedly every time the subprogram in invoked.
Mostly if you are used to C. Even so, K&R C didn't allow initialization
of automatic arrays. ANSI added that.
In many systems the executable file describes the contents of memory,
and initialized static variables just store the value in the appropriate
part of the file. I do remember (a long time ago) initializing a large
array in a DATA statement, and then finding it generated a very large
object file. I actually had it punched on cards and could see each array element.
If you are familiar with fortran, then you might be able to deduce what is most likely, but I think that is asking too much of a programmer, especially a modern programmer who must use several languages and where different languages treat this situation differently.
I remember being surprised that K&R C allowed initialized auto
variables, and that ANSI C allowed initalized auto arrays.
Fortran was there first, it is C that goofed. Not to mention
the misuse of the static and extern keywords in C.
I think a good case can be made that a warning should be issued and that the programmer should be required to make the SAVE explicit. This is what I do in my own codes, in order to avoid confusion by other programmers and by me a week after I wrote the code so I don't have to waste time remembering, or looking up, what is the convention.
Wouldn't it be more fair for C compilers to indicate that
the variable would be initialized each time if they don't
say static?
The situation that you suggested is not consistent with most of fortran's history. You wanted a stack variable that is reset on each entry. That is just not fortran semantics. Also, it is redundant with having a simple assignment at the beginning of the subroutine that is separate from the declaration.
So if you think about all the possibilities, the fortran convention is the one that is most consistent with past language standards, with the past history of (mostly) static allocation of variables, and it is the one that has the most useful semantics.
Yep.
-- glen
.
- Follow-Ups:
- Re: automatic variables are not initialized correctly?
- From: Dave Thompson
- Re: automatic variables are not initialized correctly?
- References:
- automatic variables are not initialized correctly?
- From: Nasser Abbasi
- Re: automatic variables are not initialized correctly?
- From: Tobias
- Re: automatic variables are not initialized correctly?
- From: gary . l . scott
- Re: automatic variables are not initialized correctly?
- From: Richard E Maine
- Re: automatic variables are not initialized correctly?
- From: Gary Scott
- Re: automatic variables are not initialized correctly?
- From: Ron Shepard
- automatic variables are not initialized correctly?
- Prev by Date: Re: opening files and code cleanup
- Next by Date: Re: automatic variables are not initialized correctly?
- Previous by thread: Re: automatic variables are not initialized correctly?
- Next by thread: Re: automatic variables are not initialized correctly?
- Index(es):
Relevant Pages
|