Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > alt.os.development > #8772

Re: Smaller C

From "James Harris" <james.harris.1@gmail.com>
Newsgroups alt.os.development
Subject Re: Smaller C
Date 2015-09-13 10:28 +0100
Organization A noiseless patient Spider
Message-ID <mt3fgt$jot$1@dont-email.me> (permalink)
References (15 earlier) <a9110bdf-add3-428c-b6da-2dbd52f45557@googlegroups.com> <msvds4$ig1$1@speranza.aioe.org> <op.x4tlsdpnyfako5@localhost> <mt109k$kje$1@dont-email.me> <op.x4v45jxsyfako5@localhost>

Show all headers | View raw


"Rod Pemberton" <boo@fasdfrewar.cdm> wrote in message 
news:op.x4v45jxsyfako5@localhost...
> On Sat, 12 Sep 2015 06:56:36 -0400, James Harris 
> <james.harris.1@gmail.com> wrote:
>
>> "Rod Pemberton" <boo@fasdfrewar.cdm> wrote in message
>> news:op.x4tlsdpnyfako5@localhost...
>>> On Fri, 11 Sep 2015 16:32:14 -0400, Benjamin David Lunt
>>> <zfysz@fysnet.net> wrote:
>
>>>> However, at the moment, and probably for some time, I will not
>>>> need anything like that in my OS development, and will probably
>>>> not pursue it much further than what I have done so far.  At
>>>> least not at the moment.  I will be interested to see what you
>>>> come up with, whenever you do add this functionality to yours.
>>>
>>> Well, I rarely use any qualifiers.  They're generally not needed,
>>> if you're a cautious programmer.  I've never found a use for 
>>> 'const'.
>>
>> I don't use it much but const is a good way to confirm that a certain
>> piece of data is not updated in a function.
>
> C supports pass-by-value by default and pass-by-reference via a 
> pointer.
> Just how do you modify a value passed-by-value? ...

AIUI in

  int func(int x, const int y) {
    const int w = some expression;
     ....
  }

You can modify your local copy of x in the function but the compiler 
should stop you modifying your y and w (thereby giving you assurance in 
a long function that you can always access the original values anywhere 
in that function knowing that no other part of the function code has 
changed them).

>> Whether you are a cautious
>> programmer or not, if you look at a certain function and wonder 
>> whether
>> a variable, say, is updated anywhere in the function const can 
>> confirm
>> it without you having to read through and understand the entire 
>> function
>> (as long as it's not pointed at!).
>
> When would you need that?

C allows the values passed in to be modified. Sometimes a programmer 
wants the original x that was supplied.

> I can understand the compiler being able to optimize better if
> it's present, but I personally have never had a need for that
> with my code or someone else's.

I don't know about optimisation here but it may apply.

>>> I've only used 'static' once for convenience.
>>
>> C's static can be very useful in compartmentalising code. IIRC from
>> discussions we've had before I know you don't favour data hiding so
>> it's not surprising that it's not one you use much.
>
> I use what's appropriate (IMO) for the code.  Programming is 
> complicated
> enough without making a mess of it.  Extra qualifiers, which most
> programmers have difficulty understanding and using correctly, is one
> such example.  How many times have you had to fix C code that had
> incorrect 'const' and 'static' plastered everywhere?

I haven't had to fix such code but I don't often work on the code that 
others have written. By "plastered everywhere" (LOL) perhaps you are 
weighting the argument with an emotional pejorative. ;-)

>> In a function static says, essentially, to place the declaree in the
>> data section (yet only visible in this function) rather than in the
>> function's activation record.
>
> Deja Vu!
>
> Now, technically, C doesn't require an activation record, so
> to be proper ...

AFIACS all programs with callable functions require the use of 
activation records so that they can execute. The C *standard* may not 
refer to them because it is a lanuage definition but a C *compiler* will 
certainly generate them.

> s/function's activation record/auto storage/

When a function is called its autos (and other things) will be stored in 
an activation record.

> [Sigh, this should really remind you of what I just said to Alexei 
> ...]
>
>
> AFAIR, C doesn't have any "activation records".  So, what is a
> "function's activation record"?  Google says an "activation
> record" is the same as a "stack frame".

It often is a stack frame but an activation record does not *need* to 
sit on a stack. If a function cannot be called recursively then, 
technically, its activation record could be a preassigned place in 
memory. I think Fortran works that way.

Further, if a function can be called recursively (directly by it calling 
itself or indirectly by it calling something else which eventually leads 
to it being called again *before* the thing it called returns) its 
activation record can be placed in some arbitrary place in memory and 
linked to other activation records to form a chain. The ARs are still 
processed in a FIFO fashion so even though the implementation is a 
linked list you could see this as a stack of ARs.

> Deja Vu again!  I'd
> swear I posted a similar response in the past ...  Hence, the same
> comment as was said to Alexei, but modified for you.  You've used
> this term before.  Does this term come from your IBM mainframe
> background that you "cut [your] teeth on"? ...  Upon?

No, I think it comes from studying compilers. Do you have another name 
for activation records?

> That aside.  Yes, use of 'static' on a variable limits usage, i.e.,
> scope, to that procedure.  Although, the data is preserved across
> procedure calls, just like a file scope variable, instead of being
> created and destroyed like a procedure 'auto' variable.  So what
> real advantage does a 'static' variable have over a file scope
> variable?  (rhetorical, i.e., I think the answer is: 'none'.)

I'll answer anyway! In

  int f(....) {
    static int aa;

the variable aa is protected against modification from any other 
function. It's like a global but one that no other function can change.

> Unfortunately, the 'static' keyword has at least four other uses,

Four *other* uses? I thought you had four in total. You've now found 
*five*! ;-(

> two of which have to do with linking.  That opens up the opportunity
> for misuse and errors of the keyword 'static', which can be avoided
> by simply not using 'static'.

On the contrary, use of "static" helps prevent misuse.

>> Outside a function static says to keep the declaree private and
>> not share it with other source files. IME that can be useful
>> for variables and functions.
>
> If the other source files are "unaware" of the variable, because
> the programmer did not declare said variable as an 'extern' in the
> other source files, how would the source file "know" of said variable?

Consider

  file1.c
    int aa;
    static int bb;
    void ff(....) { .... }
    static void gg(....) { .... }

  file2.c
    int aa; <== error
    static int bb; <== no error
    void ff(....) { .... } <== error
    static void gg(....) { .... } <== no error

Untested but AIUI the names bb and gg will be local to the files they 
are defined in. They can never conflict with names in other files, no 
matter how large the project gets or how many programmers are working on 
it. I find that's particularly useful for functions. If I write a helper 
function in one file I can declare it static to make sure its name 
doesn't leak out and it cannot be referenced from other files.

James

Back to alt.os.development | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-07-11 20:39 -0700
  Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-07-12 04:24 -0400
    Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-07-12 03:17 -0700
      Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-07-13 03:28 -0400
    Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-07-12 19:12 +0100
      Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-07-12 17:09 -0700
      Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-07-13 11:10 +0100
        Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-07-13 23:13 -0400
          Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-07-14 09:08 +0100
            Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-07-15 02:23 -0400
  Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-08-15 02:12 -0700
    Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-06 15:54 -0700
      Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-07 12:19 -0700
        Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-07 13:30 -0700
          Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-09 19:38 -0700
            Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-09 19:49 -0700
            Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-09 20:58 -0700
            Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-10 01:45 -0700
              Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-10 10:45 -0700
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-10 23:20 -0700
                Re: Smaller C "wolfgang kern" <nowhere@never.at> - 2015-09-11 09:26 +0200
                Re: Smaller C "wolfgang kern" <nowhere@never.at> - 2015-09-11 09:50 +0200
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-11 00:58 -0700
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 17:38 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-11 23:39 +0100
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 19:39 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-12 11:22 +0100
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-12 02:21 -0700
                Re: Smaller C "wolfgang kern" <nowhere@never.at> - 2015-09-12 21:53 +0200
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 03:37 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-13 09:49 +0100
                Re: Smaller C "wolfgang kern" <nowhere@never.at> - 2015-09-13 12:58 +0200
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 07:32 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-13 19:05 +0100
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-14 13:19 +0100
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-15 00:01 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-18 14:48 +0100
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-26 16:23 -0400
                Re: Smaller C James Harris <james.harris.1@gmail.com> - 2015-09-27 00:23 +0100
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-26 16:37 -0700
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-27 10:17 -0400
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-27 10:24 -0400
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-27 10:46 -0400
                Re: Smaller C James Harris <james.harris.1@gmail.com> - 2016-01-16 16:14 +0000
                Re: Smaller C Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-01-23 13:20 -0500
                Re: Smaller C James Harris <james.harris.1@gmail.com> - 2016-01-27 13:52 +0000
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-27 10:28 -0400
                Re: Smaller C James Harris <james.harris.1@gmail.com> - 2016-01-16 16:31 +0000
                Re: Smaller C Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-01-23 13:20 -0500
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-18 16:21 +0100
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-20 17:25 -0700
                Re: Smaller C "wolfgang kern" <nowhere@never.at> - 2015-09-13 12:43 +0200
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-13 09:21 +0100
                Re: Smaller C "wolfgang kern" <nowhere@never.at> - 2015-09-13 13:02 +0200
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-13 19:09 +0100
                Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-11 13:32 -0700
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 18:51 -0400
                Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-11 18:16 -0700
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-12 02:36 -0700
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-12 11:56 +0100
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 03:45 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-13 10:28 +0100
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-13 02:53 -0700
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 11:17 -0400
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-13 16:54 -0700
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 21:39 -0400
                Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-13 20:31 -0700
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 09:03 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-13 19:48 +0100
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 21:33 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-14 23:17 +0100
                Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-20 17:37 -0400
                Re: Smaller C "James Harris" <james.harris.1@gmail.com> - 2015-09-20 23:46 +0100
  Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-11 21:37 -0700
    Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-11 22:29 -0700
      Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-12 03:25 -0700
        Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-12 11:18 -0700
          Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-12 11:39 -0700
          Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 03:47 -0400
            Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 11:31 -0400
          Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-13 02:00 -0700
            Re: Smaller C "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-13 11:31 -0400
            Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-13 09:15 -0700
    Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-12 02:45 -0700
      Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-12 10:55 -0700
  Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-15 19:23 -0700
    Re: Smaller C "Alexei A. Frounze" <alexfrunews@gmail.com> - 2015-09-16 03:03 -0700
      Re: Smaller C "Benjamin David Lunt" <zfysz@fysnet.net> - 2015-09-16 10:17 -0700

csiph-web