Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #103465
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | David Brown <david.brown@hesbynett.no> |
| Newsgroups | comp.lang.c |
| Subject | Re: How to compile several C files |
| Date | Wed, 08 Feb 2017 14:58:31 +0100 |
| Organization | A noiseless patient Spider |
| Lines | 112 |
| Message-ID | <o7f82s$c2d$1@dont-email.me> (permalink) |
| References | <ddcc4bba-8f05-4537-9deb-1d5c759602f5@googlegroups.com> <o7cnp0$esp$1@dont-email.me> <af4dd6a4-bfa9-4865-aebb-ea5e0c9c25e2@googlegroups.com> <137458b9-1471-27ef-8c1e-8d9e781f315f@verizon.net> <5b6f9245-8bdf-4365-8a73-201c192f6980@googlegroups.com> <o7cvk5$hmt$1@dont-email.me> <8760klby18.fsf@bsb.me.uk> <o7erj0$1ob$1@dont-email.me> <o7f1q1$m6q$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Wed, 8 Feb 2017 13:56:44 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="aac8a8afec3dd03b3e63b75c4406aa2d"; logging-data="12365"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VVkmgoLrFC2BwWiCvAQ3rSqQfMAC7j2E=" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 |
| In-Reply-To | <o7f1q1$m6q$1@dont-email.me> |
| Cancel-Lock | sha1:8kyqzg0zb2vmidz9wKWEcl1Lijg= |
| Xref | csiph.com comp.lang.c:103465 |
Show key headers only | View raw
On 08/02/17 13:11, Richard Heathfield wrote: > On 08/02/17 10:25, David Brown wrote: >> 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! > > (or rather, don't identify objects at file scope!) > >> 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. > > True. With internal linkage, the mess is limited to one translation > unit. With external linkage, it could be anywhere. > >> 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. 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. > > I always wondered about static data in C++ classes. The whole point of a > class is that each object knows all it needs to know about *itself*. A > static object in a C++ class has just one value that is shared across > all objects of that class. Yes, and that can be very useful. (Since a C file with static data is roughly like a singleton class, it does not matter if you view that data as class static data or normal class data.) > > A friend of mine, who was a student at the time, once tried to explain > to me why it was a good idea. "Imagine a 'Student' class," he said. (I > suspect it was a familiar example to him at the time!) He explained that > it would be very useful for there to be a static member called, say, > NumStudents, which would record how many students there actually are, so > that every Student object would always know how many Student objects exist. > > "You're a student, right?" > > He acknowledged the fact. > > "Exactly how many students are there at your university?" > > "How should I know? Oh, I see." Maybe it's a very social university, and you want methods like "send_party_invitations_to_everyone". Often the static data will be for internal use, rather than public. The static "NumStudents" member could be private and used to create unique student ids. Sometimes it would make more sense to have a "university" class, and have every "student" object be attached to a "university" object in some way - the "university" object would then track the number of students. In effect, static member data simply gives you a convenient place to have a singleton object that is automatically attached to all members of the class, without having to define that "master" class. (Static member functions and static const/constexpr data have plenty of uses.) > >> "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. > > Yes. Sometimes one has no choice - one arrives on a project to find that > they /already/ have a zillion such identifiers, and there isn't the time > or the budget to fix the problem (although there always seems to be > plenty of time and budget to fix the second-generation problems that > ensue!). Ah, yes - "blame it on the predecessor" is a good reason for any dodgy programming practice :-) > > I /have/ defined a file scope non-const with external linkage in real > world code. Once ever, I think. It was very, very evil, but for that > particular project it was still the lesser of two evils. The alternative > was to add an extra parameter to every single function in the library, > and that included rewriting qsort() and bsearch() to support comparison > functions with three parameters. > In my type of programming, it is a good deal more common. But that is because I have complete control of the files that will use the data, because I write them too, and because passing things around with function parameters and pointers can be too much overhead on small microcontrollers. Reading a variable "currentSpeed" that has external linkage might take one clock cycles - calling the function "getCurrentSpeed" to read a static (internal linkage) variable in another module might take a dozen clock cycles and lose more due to register shuffling and lost optimisations on the caller site. That can matter in my line of work. It might be evil, but it can be a necessary evil. (Improvements in link-time optimisation are reducing this sort of overhead - I hope in the future to be able to use LTO so that I can use an inter-module getCurrentSpeed function that results in the same optimal code as using globals.)
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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