Related articles |
---|
[13 earlier articles] |
Re: another C-like language? was Compilers :) Keith.S.Thompson+u@gmail.com (Keith Thompson) (2023-01-09) |
Re: another C-like language? was Compilers :) david.brown@hesbynett.no (David Brown) (2023-01-10) |
Re: another C-like language? was Compilers :) gah4@u.washington.edu (gah4) (2023-01-10) |
Re: another C-like language? was Compilers :) tkoenig@netcologne.de (Thomas Koenig) (2023-01-11) |
Re: another C-like language? was Compilers :) 864-117-4973@kylheku.com (Kaz Kylheku) (2023-01-11) |
Re: another C-like language? was Compilers :) findlaybill@blueyonder.co.uk (Bill Findlay) (2023-01-11) |
Re: another C-like language? was Compilers :) david.brown@hesbynett.no (David Brown) (2023-01-11) |
Re: back in the 60s, another C-like language? was Compilers :) gah4@u.washington.edu (gah4) (2023-01-11) |
Re: another C-like language? was Compilers :) laguest@archeia.com (Luke A. Guest) (2023-01-13) |
Re: another C-like language? was Compilers :) gneuner2@comcast.net (George Neuner) (2023-01-13) |
Re: another C-like language? was Compilers :) 864-117-4973@kylheku.com (Kaz Kylheku) (2023-01-14) |
Re: another C-like language? was Compilers :) marblypup@yahoo.co.uk (marb...@yahoo.co.uk) (2023-01-15) |
Re: another C-like language? was Compilers :) marblypup@yahoo.co.uk (marb...@yahoo.co.uk) (2023-01-15) |
[1 later articles] |
From: | David Brown <david.brown@hesbynett.no> |
Newsgroups: | comp.compilers |
Date: | Wed, 11 Jan 2023 13:38:09 +0100 |
Organization: | A noiseless patient Spider |
References: | 23-01-001 23-01-002 23-01-003 23-01-008 23-01-016 23-01-029 23-01-033 23-01-034 |
Injection-Info: | gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="95332"; mail-complaints-to="abuse@iecc.com" |
Keywords: | C, standards |
Posted-Date: | 11 Jan 2023 18:10:35 EST |
Content-Language: | en-GB |
In-Reply-To: | 23-01-034 |
On 11/01/2023 00:13, gah4 wrote:
> On Tuesday, January 10, 2023 at 2:16:32 PM UTC-8, David Brown wrote:
>
> (snip)
>> The point is that you do not declare a variable until you actually have
>> something to put in it. You never have this semi-alive object floating
>> around where it is accessible, but has no valid or known state. You
>> never have an artificial initialisation, such as putting 0 in a variable
>> declared at the top of the function, in the mistaken believe that it
>> makes code somehow "safer".
>
> Java requires that the compiler be able to figure out that a variable
> (well, scalar variable) is given a value before it is used. Most of the
> time, that works out fine. Once in a while, I know that it is given
> a value, but the compiler doesn't. In that case, it is initialized
> to (usually) 0, and a comment indicating why.
>
The same applies to C and C++ programming, when using static error
checking. (And during development, you should definitely be using a
compiler capable of spotting missing initialisations, and you should
treat such warnings as bugs in your code.) And like Java tools, C and
C++ compilers are not /quite/ perfect :-)
So I agree that there are occasional uses for such "artificial"
initialisation. There are also occasions when declaring a variable
without initialising makes sense because you will later set its value
inside a conditional.
But in general, declaring and initialising at the same time is better
(IMHO). And artificial zero initialisation is a bad thing, precisely
because it effectively disables the kind of warning checks you get in
Java and in C/C++ with an appropriate compiler or linter.
> (snip)
>
>> [Variables at the top probably comes from Algol60 via Pascal. For assembler,
>> depends on the assembler. Lots of them let you have several sections in the
>> program and switch between the code and data sections as you go. IBM mainframe
>> assemblers had this feature in the 1960s. -John]
>
> Most of the IBM mainframe assembly code I know, puts the variables
> at the bottom.
That's new to me - but I have no experience with mainframes. In all the
assembly I have done (lots of different microcontrollers), I have always
had the data declared before the code that uses them. I've alternated
between data and code sections, but not within functions.
But maybe this is because most of the small microcontrollers I
programmed were pretty hopeless at dealing with data on a stack, and it
was normal to put local variables in data sections - you have static
addressing, rather than through base pointers or frame pointers. It is
quite different from how you work with "big" processors - even in the
days when the "big" processors were slower and had less memory than
modern "small" processors, if you understand what I mean.
Thanks for the history titbits here. It's always fun to hear about.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.