Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.compilers > #3296
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.compilers |
| Subject | Re: C scopes, another C-like language? was Compilers :) |
| Date | 2023-01-09 18:12 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <23-01-028@comp.compilers> (permalink) |
| References | (2 earlier) <23-01-003@comp.compilers> <23-01-008@comp.compilers> <23-01-020@comp.compilers> <23-01-024@comp.compilers> <23-01-025@comp.compilers> |
On 09/01/2023 04:48, Hans-Peter Diettrich wrote: > On 1/8/23 8:21 PM, David Brown wrote: > >> In other words, it can combine all the variables declared in nested >> scope and act as though they were all defined at the start of the >> function. > > AFAIR nested scopes were introduced just to allow for space saving > memory overlays. Regardless of whether a compiler really takes that > optimization *option*. I don't know the history as to /why/ block scope variables were introduced in C - that was all before my time. But they certainly allow you to write better structure in your code (in my opinion, anyway - I am aware that some people think "declare all variables at the start of the function before any code" gives better code). IMHO, local variables should have as small a scope as practically possible. So even if I have to use C90 (which I dislike - C99 was a big improvement), if I have a variable that is only needed inside a loop or a conditional, I'll declare it there. Compilers have always been free to use lifetime analysis to merge or overlap variables, regardless of how and where they are declared inside a function. There have never been any requirements from the language forcing compilers to have separate "stack slots" for variables that are declared at the start of a function or within the same block. Of course, compilers of old were not as sophisticated as modern compilers, and debuggers of old were also more limited, so it was quite common to have fixed and dedicated stack slots for each variable declared at the start of the function. But it was never /required/ for correct implementation of the language. > Of course problems can arise from malware assuming memory contents as > left over from a previous block, as it's not required that the compiler > initializes all local variables on block entry. > You are required to initialise or assign to all local variables that you read. Failing to do so is undefined behaviour (or in some circumstances, unspecified behaviour). And it doesn't matter whether you initialise your local variable at the start of a block, or assign to it later when you use it - the compiler is under no obligation to make any initialisation at the start of the block, regardless of what you write. It only has to make sure you get the same results in the end as you would have had if it had followed your source code directly. If malware can execute its code inside your functions (from code injections, buffer overflows, etc.), then all sorts of things can go wrong. Initialising local variables at the start of a block can reduce the risks and consequences a bit, but it will not save you.
Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar
Compilers :) "Tristan B. Velloza Kildaire" <deavmi@redxen.eu> - 2023-01-02 12:28 +0200
Re: Compilers :) Spiros Bousbouras <spibou@gmail.com> - 2023-01-02 20:52 +0000
Re: another C-like language? was Compilers :) Steve Limb <stephenjohnlimb@gmail.com> - 2023-01-03 16:24 +0000
Re: another C-like language? was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-03 12:52 -0800
Re: another C-like language? was Compilers :) arnold@skeeve.com (Aharon Robbins) - 2023-01-04 17:12 +0000
Re: another C-like language? was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-04 12:39 -0800
Re: another C-like language? was Compilers :) "marb...@yahoo.co.uk" <marblypup@yahoo.co.uk> - 2023-01-05 06:27 -0800
Re: another C-like language? was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-05 16:26 -0800
Re: another C-like language? was Compilers :) David Brown <david.brown@hesbynett.no> - 2023-01-06 15:39 +0100
Re: another C-like language? was Compilers :) Kaz Kylheku <864-117-4973@kylheku.com> - 2023-01-09 17:41 +0000
Re: another C-like language? was Compilers :) David Brown <david.brown@hesbynett.no> - 2023-01-10 17:48 +0100
Re: another C-like language? was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-10 15:13 -0800
Re: another C-like language? was Compilers :) David Brown <david.brown@hesbynett.no> - 2023-01-11 13:38 +0100
Re: back in the 60s, another C-like language? was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-11 16:38 -0800
Re: another C-like language? was Compilers :) "marb...@yahoo.co.uk" <marblypup@yahoo.co.uk> - 2023-01-15 04:26 -0800
Re: another C-like language? was Compilers :) Kaz Kylheku <864-117-4973@kylheku.com> - 2023-01-11 11:02 +0000
Re: Scheme is not another C-like language? was Compilers :) George Neuner <gneuner2@comcast.net> - 2023-01-12 02:54 -0500
Re: another C-like language? was Compilers :) Bill Findlay <findlaybill@blueyonder.co.uk> - 2023-01-11 11:58 +0000
Re: another C-like language? was Compilers :) Thomas Koenig <tkoenig@netcologne.de> - 2023-01-11 10:49 +0000
Re: another C-like language? was Compilers :) "marb...@yahoo.co.uk" <marblypup@yahoo.co.uk> - 2023-01-15 04:21 -0800
Re: another C-like language? was Compilers :) Andy Walker <anw@cuboid.co.uk> - 2023-01-15 22:01 +0000
Re: another C-like language? was Compilers :) "Luke A. Guest" <laguest@archeia.com> - 2023-01-13 18:25 +0000
Re: another C-like language? was Compilers :) George Neuner <gneuner2@comcast.net> - 2023-01-13 17:20 -0500
Re: another C-like language? was Compilers :) Kaz Kylheku <864-117-4973@kylheku.com> - 2023-01-14 19:07 +0000
Re: another C-like language? was Compilers :) "marb...@yahoo.co.uk" <marblypup@yahoo.co.uk> - 2023-01-07 02:14 -0800
Re: another C-like language? was Compilers :) David Brown <david.brown@hesbynett.no> - 2023-01-08 20:21 +0100
Re: another C-like language? was Compilers :) Hans-Peter Diettrich <DrDiettrich1@netscape.net> - 2023-01-09 04:48 +0100
Re: C scopes, another C-like language? was Compilers :) David Brown <david.brown@hesbynett.no> - 2023-01-09 18:12 +0100
Re: another C-like language? was Compilers :) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-09 11:24 -0800
Re: Compilers :) "Tristan B. Velloza Kildaire" <deavmi@redxen.eu> - 2023-01-13 13:41 +0200
Re: Compilers :) Hans-Peter Diettrich <DrDiettrich1@netscape.net> - 2023-01-05 01:12 +0100
Re: Compilers :) "Tristan B. Velloza Kildaire" <deavmi@redxen.eu> - 2023-01-13 14:17 +0200
Re: C and Java, was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-13 10:32 -0800
Re: C and Java, was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-13 12:39 -0800
Re: C and Java, was Compilers :) dave_thompson_2@comcast.net - 2023-01-28 10:37 -0500
Re: C and archtecture, C and Java, was Compilers :) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-29 19:37 -0800
Re: C and Java, was Compilers :) gah4 <gah4@u.washington.edu> - 2023-01-29 21:39 -0800
csiph-web