|
|
Log in / Subscribe / Register

My advice on implementing stuff in C:

My advice on implementing stuff in C:

Posted Oct 18, 2010 8:51 UTC (Mon) by marcH (subscriber, #57642)
In reply to: My advice on implementing stuff in C: by cmccabe
Parent article: Russell: On C Library Implementation

> In C and C++, functions and variables that are declared "file static" can't be referenced outside that file. If you combine that with a sane policy about how to split up functionality between multiple files, it does of the things that module systems do in other languages.

Sorry but this is not good enough: it does not scale. Whereas most module systems allow nesting, "static" in C gives you only one level. And this puts severe constraints on how you split your project into files.

> Java designers believed that they were beyond the need for a macro system. The result was that an ugly mass of automatic code generators got written to do the things that a macro system would have done.

While I share the hate for these code generators, a macro system does not seem like the solution to me. Examples?


to post comments

My advice on implementing stuff in C:

Posted Oct 18, 2010 19:17 UTC (Mon) by cmccabe (guest, #60281) [Link] (1 responses)

> Sorry but this is not good enough: it does not scale. Whereas most module
> systems allow nesting, "static" in C gives you only one level. And this
> puts severe constraints on how you split your project into files.

It's easy enough to split your project into multiple libraries. Then each library can do its own self-contained thing. Hopefully the graph of dependencies is a tree. I have programmed C in this style before. Build systems like CMake make it easy.

> While I share the hate for these code generators, a macro system does not
> seem like the solution to me. Examples?

Macros can be used for:

* Initializing structs in a very repetitive way.

* Generating short functions that do something trivial, like accessors. High level languages like ruby have :attr_accessor, but that relies on metaprogramming which isn't available in a low-level language like C.

* Generic programming

My advice on implementing stuff in C:

Posted Oct 19, 2010 11:36 UTC (Tue) by nix (subscriber, #2304) [Link]

A better alternative for short functions that do something trivial is static inline functions. You can't make something a macro unless it's just shortening something the user can do anyway, and for *that* you generally want static inlines in a header file. A decent compiler will inline them for you if appropriate.

What macros really can do that nothing else can is anything involving stringization or token pasting, e.g. filling up a structure statically with information describing other C identifiers (fields in some other structure, that sort of thing). Thanks to stringization you can fill in the *name* of the identifier and its size without worrying about skew.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds