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


Groups > comp.lang.c > #103466

Re: How to compile several C files

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: How to compile several C files
Date 2017-02-08 15:09 +0100
Organization A noiseless patient Spider
Message-ID <o7f8oa$etb$1@dont-email.me> (permalink)
References (4 earlier) <5b6f9245-8bdf-4365-8a73-201c192f6980@googlegroups.com> <o7cvk5$hmt$1@dont-email.me> <8760klby18.fsf@bsb.me.uk> <o7erj0$1ob$1@dont-email.me> <87o9ycanl0.fsf@bsb.me.uk>

Show all headers | View raw


On 08/02/17 13:12, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
> 
>> On 07/02/17 20:28, Ben Bacarisse wrote:
>>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>> <snip>
>>>
>>>> RULE ONE: DO NOT USE FILE SCOPE OBJECTS!
>>>
>>> I think it would be better to unravel these intertwined concepts.
>>> Objects don't have scope, they have lifetime.  It's names that have
>>> scope (and sometimes linkage).  What most people call "globals" are
>>> names of objects defined at file scope.  This gives the objects static
>>> storage duration (even if not declared static) and gives the names (if
>>> not declared static) external linkage.
>>
>> There is a big distinction between objects defined at file scope with
>> "static", and those defined at file scope /without/ "static".  The
>> lifetime is the same, the scope of the identifier is the same, but the
>> linkage is different.
> 
> Yes, I made that distinction but deliberately played it down because I
> think it sets the same trap for beginners.
> 
>> File static data can often be extremely useful.  It lets you have
>> controlled access to the data from a known set of functions (those in
>> the same file), without having lots of pointers passed around as
>> function parameters.
> 
> And that's the problem -- it's easier.  Obviously the situation is more
> chaotic if the names have external linkage because they can be accessed
> from other translation units, but often the damage has been done simply
> by that fact that a bunch of functions now communicate via shared names
> rather than parameters and/or return values.
> 
> It's all too easy to downplay the role of experience in programming.  I
> am sure that when you do this you have considered the alternatives and
> the future direction the project might take.  (Will there be threading
> one day?  Will you ever want multiple instantiations of this
> "module"?...)  The trouble comes when it's simply seen as the obvious
> way for two functions to operate on some data.
> 

I can accept that.  In my code, I generally have a very clear idea of
what future directions will bring, and I often have an emphasis on
efficiency (run-time speed and/or code size) rather than flexibility for
the future.

> This is why rules are so hard to come by in programming.  

Yes.  For any question of the form "what's the best way to do this?", or
"is this a good idea?", the answer is invariably "it depends".

> C has no
> modules in any modern sense, but it does have translation units with
> "internal" names and sometimes that's the best way to write a part of a
> program.  But much more often students will put data outside of a
> function just because its so much simpler.  Writing a stack?  Define an
> array at file scope (make it static to keep the teacher happy) and you
> have simple push and pop function.  But you now have a stack you can
> only have one of.

And that is absolutely /fine/ if one stack is all your program needs.
Too much generalisation can be bad too.  If I am writing a program to
control a car engine, I can make the assumption that there is only one
car and one engine, and I don't need to clutter my code with pointers to
engines in and out of every function.  If the day comes that I need to
support a two-engined car, so much is different that it is a new program
- and the refactoring can be done at that time.

I fully appreciate that students should get used to general ideas and
not "shortcuts".  But sometimes it makes sense to solve today's problems
rather than trying to solve all the potential problems of the future.

> 
>> It is somewhat like having a singleton class in
>> C++, with the static data being the data members, static functions being
>> private methods, and non-static functions being public methods.
> 
> Sure.  Knowing when that's the right design does take some time to
> develop.  Mind you, it could be argued that knowing how to convert
> between one for and another is a useful C skill.  Taking all your static
> file-scope data and putting unto a struct that gets passed about is
> something I'm sure we've all done more than once!

Yes, been there and done that.

> 
>> "globals" usually (IME) refers to file scope data that is /not/ static,
>> and thus has external linkage and can be access from other files.  That
>> is when you need to be very wary - any other files can access the data
>> so you have lost that control.  Global data like this is often viewed as
>> "evil", and that's not a bad first approximation.  It can be useful when
>> used correctly - but you need to be very careful.
> 
> The trouble is that for small programs the distinction is simply
> technical.  A one-file program has the same good or bad design if the
> file-scope names have internal or external linkage.

I think even when working on small one-file programs, students should
get in the habit of putting "static" in front of all file-scope data and
all functions, unless they have very good reason not to - and for
single-file programs, that basically means only "main" does not have
"static".

> 
>> Global constant data is much less of a problem, but like non-static
>> functions the identifiers use a program-wide namespace.  Defining a
>> non-static const array with the name "data" is a bad idea - though it
>> might be okay for static const data.
> 
> Yes, there's a whole sliding scale of problems from multiple translation
> units modifying shared globals all over the place, to a few bits of
> constant of data shared between TUs, with the latter being seen by many
> as "no problem at all" provided the names are not an issue.
> 
> <snip>
> 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 06:10 -0800
  Re: How to compile several C files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-07 07:02 -0800
  Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-07 16:03 +0100
  Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 15:06 +0000
    Re: How to compile several C files BartC <bc@freeuk.com> - 2017-02-07 15:14 +0000
      Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 16:02 +0000
    Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 15:42 +0000
      Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 16:19 +0000
        Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 16:40 +0000
          Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 17:16 +0000
            Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 10:19 +0100
        Re: How to compile several C files "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-07 11:51 -0500
      Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 08:26 -0800
        Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 16:42 +0000
          Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 10:14 -0800
          Re: How to compile several C files vitale.alla@gmail.com - 2017-02-16 09:49 -0800
      Re: How to compile several C files Ian Collins <ian-news@hotmail.com> - 2017-02-08 17:35 +1300
        Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 10:28 +0100
          Re: How to compile several C files Ian Collins <ian-news@hotmail.com> - 2017-02-08 22:43 +1300
            Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 11:29 +0100
      Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 10:14 +0100
  Re: How to compile several C files James Kuyper <jameskuyper@verizon.net> - 2017-02-07 10:07 -0500
    Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 07:56 -0800
      Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 08:24 -0800
        Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 16:44 +0000
          Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 08:54 -0800
            Re: How to compile several C files "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-07 12:20 -0500
              Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 10:05 -0800
                Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 18:27 +0000
                Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 18:40 +0000
                Re: How to compile several C files Keith Thompson <kst-u@mib.org> - 2017-02-07 12:22 -0800
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 10:33 +0100
                Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-08 10:25 -0800
      Re: How to compile several C files Keith Thompson <kst-u@mib.org> - 2017-02-07 08:58 -0800
      Re: How to compile several C files "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-07 12:07 -0500
        Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 09:14 -0800
          Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 17:21 +0000
            Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 10:14 -0800
            Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 10:28 -0800
              Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 18:43 +0000
                Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 10:55 -0800
            Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-07 19:28 +0000
              Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-07 19:51 +0000
              Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 11:25 +0100
                Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-08 12:11 +0000
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 14:58 +0100
                Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-08 14:23 +0000
                Re: How to compile several C files supercat@casperkitty.com - 2017-02-08 07:48 -0800
                Re: How to compile several C files Richard Heathfield <rjh@cpax.org.uk> - 2017-02-08 16:09 +0000
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 17:23 +0100
                Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-08 15:01 +0000
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 17:22 +0100
                Re: How to compile several C files Keith Thompson <kst-u@mib.org> - 2017-02-08 09:27 -0800
                Re: How to compile several C files supercat@casperkitty.com - 2017-02-08 09:51 -0800
                Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-08 21:06 +0000
                Re: How to compile several C files supercat@casperkitty.com - 2017-02-08 13:20 -0800
                Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-08 21:40 +0000
                Re: How to compile several C files supercat@casperkitty.com - 2017-02-08 13:49 -0800
                Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-08 23:09 +0000
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 23:25 +0100
                Re: How to compile several C files supercat@casperkitty.com - 2017-02-08 15:17 -0800
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-09 01:17 +0100
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 23:19 +0100
                Re: How to compile several C files Keith Thompson <kst-u@mib.org> - 2017-02-08 16:44 -0800
                Re: How to compile several C files Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-08 22:32 -0800
                Re: How to compile several C files Robert Wessel <robertwessel2@yahoo.com> - 2017-02-09 04:03 -0600
                Re: How to compile several C files Ian Collins <ian-news@hotmail.com> - 2017-02-10 07:52 +1300
                Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-08 12:12 +0000
                Re: How to compile several C files BartC <bc@freeuk.com> - 2017-02-08 13:09 +0000
                Re: How to compile several C files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-08 05:14 -0800
                Re: How to compile several C files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-08 15:32 +0000
                Re: How to compile several C files David Brown <david.brown@hesbynett.no> - 2017-02-08 15:09 +0100
        Re: How to compile several C files Keith Thompson <kst-u@mib.org> - 2017-02-07 09:22 -0800
          Re: How to compile several C files Gareth Owen <gwowen@gmail.com> - 2017-02-07 18:29 +0000
            Re: How to compile several C files bart4858@gmail.com - 2017-02-07 11:05 -0800
            Re: How to compile several C files Keith Thompson <kst-u@mib.org> - 2017-02-07 12:26 -0800
  Re: How to compile several C files Barry Schwarz <schwarzb@dqel.com> - 2017-02-07 08:34 -0800
    Re: How to compile several C files Alla _ <modelling.data@gmail.com> - 2017-02-07 08:57 -0800
      Re: How to compile several C files "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-07 12:29 -0500
      Re: How to compile several C files Manfred <noname@invalid.add> - 2017-02-07 19:02 +0100

csiph-web