News for hackers. Stuff that matters.
Re:Some obscure C features by WalterBright on Thursday January 25, 2018 @05:26PM (#16235308)
My not-so-humble-opinion is that if you find yourself doing metaprogramming with the C preprocessor, it's time to upgrade to a more powerful language.
Re:Some obscure C features by vardump on Thursday January 25, 2018 @05:58PM (#16235537)
Sometimes you just don't have other options than C.

Like kernel drivers, embedded (microcontroller) development.

Or perhaps you just need to develop a small loadable library without any runtime (no malloc, etc.). Or perhaps writing Python, Lua etc. C-module for better performance.

You just need to treat C with proper respect, not be too arrogant about your skills.

I hope over time we can move more and more to Rust or some other language which gives as many errors at compile time as possible and helps with not shooting at our feet.

Shouldn't have too many C-macros, of course. Some macro messes I've seen can be nearly impossible to debug...

Re:Some obscure C features by pcwalton on Thursday January 25, 2018 @06:04PM (#16235579)
> You just need to treat C with proper respect, not be too arrogant about your skills.

We've been hearing "C isn't the problem, bad programmers are the problem" for 40 years. This has continued to perpetuate the problems with the language.

I mean, I certainly agree that there are no alternatives to C now for certain domains, but there are real problems with it that aren't solved by just "treating it with proper respect".

Re:Some obscure C features by vardump on Thursday January 25, 2018 @06:09PM (#16235615)
Having programmed C for soon 30 years, I absolutely agree with you that C is the problem.

But often you just need to get the job done, and have limited options available.

Perhaps in 50-100 years, people looking back at software from this era won't be able to understand how we managed to make it work at all.

I haven't met or know anyone mastering C/C++ (especially C++), and doubt I'll ever hear about one.

Re:Some obscure C features by hedora on Thursday January 25, 2018 @10:47PM (#16236906)
I know plenty of C masters, and a few C++ masters. If you want to meet C++ masters, go to cppcon or get a job writing systems software at a respectable C++ shop.

I’d say I had a working knowledge of C++ after about a year ramping up on a codebase based on a mature C++ framework.

That was after years of writing reliable, performant C code. My C++ is now significantly better than the C I can produce, on both fronts, and at the same time.

I’m also much more productive as a side effect of that.

Re:Some obscure C features by pjmlp on Friday January 26, 2018 @07:20AM (#16238584)
I doubt very seriously that any of those C masters can recite by heart the 200 plus use cases of UB recognized by ANSI C.
Re:Some obscure C features by jstimpfle on Thursday January 25, 2018 @06:28PM (#16235746)
> I haven't met or know anyone mastering C/C++ (especially C++), and doubt I'll ever hear about one.

C can definitively be mastered. C at it's core is pragmatic minimalism. The basic ideas are very simple. If you want to be a language lawyer and know the standard completely (including all historical accidents), it can get complex, but still manageable. But anyways, you shouldn't be a language lawyer. That's not C. Make maintainable programs instead, stay in the middle of the road.

C gets out of the programmer's way. It's not about mastering C, but about mastering programming. And while I agree there's much bad C software out there: I haven't seen one maintainable non-bloated Java project. Here is one master of C that I'm sure you know: Linus Torvalds. Of course there are many, many more. Just remember it's not about C, but programming machines in general.

Re:Some obscure C features by pcwalton on Thursday January 25, 2018 @07:48PM (#16236180)
> I haven't seen one maintainable non-bloated Java project

I also haven't seen one large-scale network-facing C project written using reasonable development practices (i.e. not absurdly expensive formal verification) that hasn't had some sort of terrible remote code execution vulnerability. By contrast, Java programs tend to be far less vulnerable to this kind of problem. (The closest thing is probably bugs with attacker controlled java.io.Serializable, which while severe are much less frequent than RCE in C programs.)

> Here is one master of C that I'm sure you know: Linus Torvalds.

I agree that Linus is a master of C. Linux is also an excellent example of the above problems with C.

Re:Some obscure C features by jpfr on Friday January 26, 2018 @04:58AM (#16238050)
> I also haven't seen one large-scale network-facing C project written using reasonable development practices (i.e. not absurdly expensive formal verification) that hasn't had some sort of terrible remote code execution vulnerability.

This has gotten a lot better with static code analysis (clang-analyzer, Coverity, ...) and whitebox-fuzzing (libfuzzer, afl, ...). Both simply point you to the bugs and don't require a formal spec.

Together with Valgrind, these tools have revolutionized C programming for me.

Re:Some obscure C features by pcwalton on Friday January 26, 2018 @06:50AM (#16238445)
These tools are important for legacy code or code that cannot be written in any language other than C. They do not bring C code anywhere near to code written in other languages in terms of security and correctness.
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @07:21AM (#16238592)
Plus those tools are not available in all C compilers, specially in the embedded world.
Re:Some obscure C features by fanf2 on Friday January 26, 2018 @08:18AM (#16238879)
I believe BIND9 has never had a remote code execution vulnerability - plenty of denial of service vulnerabilities, though, because it kills itself when it detects memory corruption.
Re:Some obscure C features by jstimpfle on Friday January 26, 2018 @03:05AM (#16237656)
I wouldn't say "mastering C" should imply "never have any security issues". And many vulnerabilities are conceptual and don't have anything to do with the use of C.

Use your tools wisely. Architect programs for clear data flow. Validate inputs as soon as possible. Don't do (de)serialization by hand.

By all means use a managed "memory safe" language if you have many data accesses that are not behind a validation gateway. But for large amounts of data or complex software, that's still not an option. C is still the only language in which I can write software of advanced complexity - because it does not get in my way.

Re:Some obscure C features by pcwalton on Friday January 26, 2018 @06:51AM (#16238451)
The entire industry has been successfully writing programs that process "large amounts of data" and "complex software" in languages other than C for decades.
Re:Some obscure C features by jstimpfle on Friday January 26, 2018 @08:20AM (#16238885)
Counter-examples: Kernels, Game Engines, Software with lots of small "objects". I once had to write a SAT solver in Java (dozens of millions of clauses) and it was a disaster which I could only rescue by converting to highly unidiomatic Java code. It would have been much more straightforward in C.
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @02:14PM (#16242021)
> Kernels

Mesa/Cedar at Xerox PARC, Topaz at DEC/Olivetti, Oberon at ETHZ, JavaOS at Sun, Singularity/Midori at Microsoft Research

> Game Engines

Unity is slowly rewriting parts of their C++ pipeline with C#, after their IL2CPP compiler got mature.

Then there are game engines like Xenko being done.

Also being with one foot into games since the 80's, I remember the transitions "Pascal/C are too slow, we stick Assembly", "C++ is too slow, we stick with C", now we are in "JavaScript/C#/Java are too slow, we stick with C++" phase.

Re:Some obscure C features by vardump on Thursday January 25, 2018 @06:51PM (#16235880)
> C can definitively be mastered.

If that's true, how come there are almost no widely used network facing C programs that didn't have plenty of security vulnerabilities?

(Anything written by Daniel J. Bernstein doesn't count, because there's no way he's a mere mortal. :))

Re:Some obscure C features by hedora on Thursday January 25, 2018 @10:50PM (#16236914)
OpenBSD has done a great job, especially if you look at what they achieved, how fast they got there, and what resources were at their disposal.
Re:Some obscure C features by qznc on Friday January 26, 2018 @06:27AM (#16238377)
Qmails direct competitor Postfix? Postfix has more, but Qmail has one with a higher score: https://www.cvedetails.com/vulnerability-list/vendor_id-8450... vs https://www.cvedetails.com/vulnerability-list/vendor_id-86/p...

Postfix is also much more widely used than qmail.

Re:Some obscure C features by littlestymaar on Thursday January 25, 2018 @09:11PM (#16236529)
Even djb had flaws in his C code despite being a semi-god. It took a $10k bounty and several years to be found but it was there !
Re:Some obscure C features by shaklee3 on Thursday January 25, 2018 @08:20PM (#16236322)
Which language has? Keep in mind market share, so don't say Go.
Re:Some obscure C features by Avshalom on Thursday January 25, 2018 @07:09PM (#16235985)
for, while, do...while, recursion, goto: is not a sign of "pragmatic minimalism" unless one choses to define it tautologically as "what C has".
Re:Some obscure C features by jstimpfle on Friday January 26, 2018 @02:52AM (#16237612)
"recursion" is not a concept in C. Just function calls. "for" and "while" are very simple concepts and basically the same. They are very simple and well known idioms and not C specific. Most languages have both - not a big deal. I use "for" mostly for iterating over arrays, and I have otherwise mostly while loops and while(1) loops with manual breaking.

Goto, you just can't do without it.

Also not saying that in some cases even C is too much abstraction. But in general, its memory abstractions and its function call abstraction have proved to be very effective helping humans develop in a modular fashion.

Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:22AM (#16237907)
> Goto, you just can't do without it.

Strange, the last time I used it I was still doing Basic in the early 90's, or when I do some Assembly programming.

So yeah, we can do without it, as long as the languages are powerful enough.

Re:Some obscure C features by vardump on Friday January 26, 2018 @05:33AM (#16238188)
I do find forward gotos useful in C. For error handling and breaking out of nested scopes. But only when goto is simpler and easier to understand than the alternative.

Not saying there's no use for backward gotos, just don't remember ever needing them. Maybe they could be useful for some C code generation cases or perhaps for retrying some operation?

What really annoys me are C++ exceptions used for flow control. Surprise gotos. I loved exceptions long ago at first, but over time the feeling has shifted more and more towards hate. Exceptions make code bases so much harder to understand. Especially when combined with inheritance.

Re:Some obscure C features by vardump on Friday January 26, 2018 @05:38AM (#16238203)
> "recursion" is not a concept in C. Just function calls.

There are indeed C compilers that don't support recursion (or function re-entrancy). What does the standard say about stacks?

Re:Some obscure C features by jstimpfle on Friday January 26, 2018 @08:30AM (#16238941)
I'm not a standards guy, so I might be wrong, but this is the only reference to recursive calls that I could find from the C11 draft [1], Section 6.5.2.2 item 11:

    Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions.
Stacks, I don't know. I only care about the concept (and semantics) of function calls. The call stack is an implementation detail. I wouldn't be surprised if the concept of a call stack was not part of the standard.

[1] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Re:Some obscure C features by comex on Friday January 26, 2018 @08:54PM (#16244383)
Indeed, it's not. In the case of what we'd usually call stack-allocated variables, the C standard just uses the phrase "automatic storage duration", and defines their lifetime without explicitly referencing a stack:

> For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.

Re:Some obscure C features by hedora on Thursday January 25, 2018 @10:51PM (#16236919)
Pragmatic == close enough to all of the popular and contemporary assembler flow control constructs.
Re:Some obscure C features by red75prime on Thursday January 25, 2018 @07:55PM (#16236215)
> C at it's core is pragmatic minimalism.

Pragmatic minimalism as it was for the hardware at the beginning of the 1970s, to be exact.

Re:Some obscure C features by jstimpfle on Friday January 26, 2018 @02:45AM (#16237593)
The C machine model is still the only model I can easily program and not make a mess. And without being a hardware architecture expert: Has so much changed on the assembly level? And what other languages are truer to today's machines?
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:26AM (#16237923)
None of them are, not even Assembly, unless the CPU executes the opcodes directly in hardware without micro-code help.

The C machine model says nothing about vector instructions, instruction re-ordering, NUMA, GPGPU, IO ports, Harvard architecture, multi-core.

Re:Some obscure C features by comex on Friday January 26, 2018 @08:57PM (#16244399)
As of C11, the C machine model does (finally) include threads and memory ordering, so at least you can say it says something about multi-core…
Re:Some obscure C features by red75prime on Friday January 26, 2018 @05:31AM (#16238181)
Modern C compilers do enormous job of optimizing C code, despite its shortcomings like the lack of distinction between pointers and arrays. You don't need to worry too much about that, maybe you'll need to sprinkle vectorization intrinsics or assembler code if the compiler isn't smart enough, or to add a couple of memory barriers.
Re:Some obscure C features by vardump on Friday January 26, 2018 @05:53AM (#16238259)
Would mostly sign that, but not without inspecting security or performance critical code in disassembler.

Modern compilers are still rather bad at vectorization. What's worse, those optimizations are often unstable. A small change might make code several times slower. Performance regression tests and when that fails, vector intrinsics are a must.

Re:Some obscure C features by agumonkey on Monday February 05, 2018 @04:20AM (#16307643)
I'm curious about what other languages could be used in place. Maybe ADA ? it's both generic oriented and low level.
Re:Some obscure C features by majewsky on Friday January 26, 2018 @04:35AM (#16237955)
A friend of mine who is working in HPC research (high-performance computing) generates CUDA kernels using higher-kinded templates. He can instantiate you a

  template<template<class>, template<class, template<uint>>, class>
before breakfast.
Re:Some obscure C features by shouldgowork on Thursday January 25, 2018 @06:19PM (#16235690)
We've been hearing "how long will the problems of C persist" mere persist before people realize there might be reasons it's the most successful programming language yet invented?
Re:Some obscure C features by pcwalton on Thursday January 25, 2018 @07:46PM (#16236171)
There are definitely reasons behind C's success. Most important was that it was the language of Unix, which spread like wildfire in the '70s and '80s because it was free.

There are equally important reasons why C's usage has been in steady decline from the '90s to today.

Re:Some obscure C features by WalterBright on Thursday January 25, 2018 @08:30PM (#16236360)
> Most important was that it was the language of Unix

More important was it was the only language that really worked on DOS, which ruled the computer biz for 10 years. I would guess 80% of programming was done for DOS, and C was a very good fit for the 640K systems.

Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:32AM (#16237944)
On my corner of the planet we were using Turbo Pascal, Turbo Basic, Quick Basic, Clipper, FoxPro, DBase.

C and C++ had no special place on the spectrum of programming languages usage, unless we were porting code between MS-DOS and UNIX.

Until it was time to move into Windows, and anyone not using C or C++ started to be left behind, manually writing FFI wrappers (e.g. Turbo Pascal/Delphi) and we eventually migrated to one of them.

Re:Some obscure C features by sedachv on Friday January 26, 2018 @04:46AM (#16237997)
None of that is true at all. Turbo Pascal was hugely popular on DOS in the 1980s into the early 1990s. It wasn't until Windows 3.1 that Turbo Pascal really started losing market share to C. You are also completely forgetting about Apple. IBM PC compatibles did not even pass 50% market share until 1986.
Re:Some obscure C features by pcwalton on Friday January 26, 2018 @01:14AM (#16237361)
Well, sure, but Microsoft wouldn't have chosen to promote C if not for the success of Unix. :)
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:27AM (#16237926)
Quite true, and on those days Microsoft was equally promoting Quick Basic and Quick Pascal, while writing MS-DOS in pure Assembly.
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:28AM (#16237931)
No, C got famous thanks to UNIX, just like we have to use JavaScript thanks to the browsers.
Re:Some obscure C features by Avshalom on Thursday January 25, 2018 @07:00PM (#16235936)
Path dependency is why. It's not complicated, it's just expensive to overcome.
Re:Some obscure C features by akvadrako on Friday January 26, 2018 @03:09AM (#16237670)
I think only recently has a language been invented which has a good chance of replacing C and that's Swift. It's like C++ done right, it's user friendly, pragmatic, backed by a huge company and doesn't use GC.
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:32AM (#16237945)
Reference counting is a GC algorithm.
Re:Some obscure C features by audunw on Friday January 26, 2018 @03:28AM (#16237745)
> Sometimes you just don't have other options than C.

This is true. With all the new and fancy languages around, it's strange that there are so few attempts at making a better C.

I would say that the characteristics of C is: dead simple, manual memory management, "fancy assembly".

Rust is close, but fails on being dead simple.

The best attempt I know of, is Zig: http://ziglang.org/

But it's still very early in development. I am using it for microcontroller code right now though, and it's much nicer than C already, despite having to deal with bugs.

Re:Some obscure C features by nickpsecurity on Thursday January 25, 2018 @07:05PM (#16235966)
"Sometimes you just don't have other options than C."

You always have other options if better languages can compile to C or integrate it via FFI. There's been plenty to do that, too. For simple and safe, Modula-2 was early one compiled to C that couldve had syntax modified to be more like it. If aiming for power, PreScheme was a systems dialect of Scheme that compiled to C at one point. The OcaPIC (Ocaml) project and use of ATS language for 8-bit show stuff like that could probably be made to wofk in areas C is used in, too.

So, I'd say it's a myth you need to develop in C just because a target has only C libraries or compilers that safer languages can use. Instead, we get a new claim that people just didnt do it for reasons that varied per project or person but wasn't technical capability.

Re:Some obscure C features by kurtisc on Thursday January 25, 2018 @08:34PM (#16236374)
For the microcontrollers example, the 'C' that the device's compiler uses could be a very loose implementation of a subset of the language. How do you ensure that the code you're compiling to will work for it?
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:35AM (#16237956)
Depends on the microcontroler.

The majority of them also enjoy Pascal and Basic compilers.

https://www.mikroe.com/

So sometimes, there is indeed an option.

Also many languages have compilers that are able to generate C code.

As to ensuring, well that is what testing is for.

Re:Some obscure C features by nickpsecurity on Thursday January 25, 2018 @09:06PM (#16236511)
That's a good point. I doubt most languages generating C put a lot of thought into what subset is supported in proprietary compilers. If anyone tries one, then there could be profiles done for unusual compilers where the generated code has certain properties. Far as the past, I think people couldve tested the output just to see what happened. Then, either ditch high-level language per fuction, module, or project depending on how far incompatibility effects went.

How does that sound?

Re:Some obscure C features by flavio81 on Thursday January 25, 2018 @07:33PM (#16236114)
>Sometimes you just don't have other options than C. Like kernel drivers, embedded (microcontroller) development

Use Common Lisp to generate C code. There are three libs that are specific for this, see C-mera for example.

Re:Some obscure C features by pjmlp on Friday January 26, 2018 @04:33AM (#16237946)
That is how the .NET GC was originally implemented.
Re:Some obscure C features by hedora on Thursday January 25, 2018 @10:55PM (#16236934)
Wait. If I use non-tail recursion on a micro controller, then I’ve probably implemented a stack out of memory crash, which, for embedded devices, is actually worse than a buffer overflow on unsanitized input.

Is this a joke, or do they do some sort of stack-bounding static analysis?

Re:Some obscure C features by sedachv on Friday January 26, 2018 @04:22AM (#16237910)
What are you talking about? Running out of stack has nothing to do with what you use for code generation/preprocessing. I also don't see why running out of stack would ever be worse than a buffer overflow. The former is at least going to be fail-fast.
Re:Some obscure C features by quicknir on Friday January 26, 2018 @10:13AM (#16239716)
I keep hearing this. How exactly common is it that you can't use C++ for technical, as opposed to human, reasons? gcc is by far the most talked about compiler in this space (at least from what I hear) and obviously it's a C++ compiler as well.

My guess is that in most (not all) cases C++ is usable instead of C, but the culture of such development is to prefer C. It's a real pity, because for someone with a moderate amount of judgement (i.e. not going off the deep end on unnecessarily complicated C++), it only takes a moderate amount of C++ knowledge to be able to write more maintainable and correct code that performs equally well.

Re:Some obscure C features by Koshkin on Saturday January 27, 2018 @10:30AM (#16246533)
What happens in reality though is that as soon as developers get access to the stanard library all hell breaks loose. People switch to the Java mode of thinking and start almost mindlessly using vectors, maps, shared pointers, etc. and C-level performance goes out of the window. With their exposure to templates, there goes their sanity and, as a consequence, code maintainability.
Re:Some obscure C features by stochastic_monk on Thursday January 25, 2018 @08:00PM (#16236237)
Exactly. I like C++ because it lets me abstract over the absolute fastest C-like code.

For example, to produce generic SIMD-accelerated code over both Intel-specific SIMD operations and the Sleef vector math library I needed for FHT-based JL-transforms and kernel projections, I used template specialization to abstract to operations on physical memory. [0]

The generic methods themselves were gross, but in application, it was extremely simple in downstream code to specify an operation and a container and have the compiler pick the fastest method for the job. I’ve also checked my generated assembly and the smaller functions are always inlined, even though they’re called from static constexpr function pointers.

This is also a great use case for macros, as they saved me huge amounts of typing.

I’m sure many will say C++ is the wrong direction to go for metaprogramming, but it’s worked great for my purposes and lets me get straight to the hardware.

[0]: https://github.com/dnbaker/frp/blob/master/include/frp/vec.h

Re:Some obscure C features by stochastic_monk on Saturday January 27, 2018 @03:00PM (#16247813)
As a heads-up, in case anyone checks, I’ve moved this to its own repository so I can use it in other projects easily: https://github.com/dnbaker/vec.
Re:Some obscure C features by pcwalton on Thursday January 25, 2018 @06:01PM (#16235560)
Agreed. A sad amount of the history of Unix has consisted of gradually migrating ill-advised solutions made with text-pasting preprocessors to more sensible designs. See the Bourne shell (written in Bournegol), autoconf (written in m4), troff, sysvinit, etc.
Re:Some obscure C features by mort96 on Thursday January 25, 2018 @05:59PM (#16235546)
I don't think I agree with your point in the general case, but even if you were 100% correct, and nobody should do metaprogramming with the C preprocessor in regular production code, I still don't think that's very relevant. The specific use case I had for all of these features were a unit testing library, which is very different from a regular program. If you need a testing library for C, that's probably because you have C code. You probably shouldn't try to write a test suite in Python for your C library.
Re:Some obscure C features by nickpsecurity on Thursday January 25, 2018 @07:15PM (#16236017)
Why not? The majority of engineering professions say use the right tool for the job. Whatever is most cost-effective. Programming is one of few things where people try to do the opposite. Fortunately, many in the profession do right tool for the job philosophy at least for whole domains like OS code, web apps, and so on. Still an insistence on sticking to same language too much.

Python has incredible productivity and versatility with ease of learning. It's readable. It's easier to prototype things like verification tools in it than C. There's also tools like Hypothesis to test that. Then, there's tools to speed up the code like Cython.

If anything, Python would be a safer, faster way to write tooling for modifying or testing C code than C itself. One could also convert Python generated tests to a C library of tests with automatic validation of data types, coverage, etc. So, I think it would make sense if someone did anything from prototyping C in Python to using it to test C code.

And do note Ive enjoyed reading your experimentation with macros in C. I learned about defer through it. Just commenting on that one point.

Re:Some obscure C features by hedora on Thursday January 25, 2018 @11:14PM (#16236989)
How is python safer than C? I deal with a code base that’s 10x more C++ than python, and when I’m on call to babysit the test clusters, >99% of the type errors are in python. With the occasional oddball exception to break the rule, the C++ type/memory errors are easier to track down to boot. (Python has its doozies too, BTW)

I think this is because writing python code is like writing C++ without templates, but all the integer types declared “auto” and all the pointers void.

The main difference is that the C++ compiler still* does more type checking than python even after you abuse the warning flags enough to get it to stop gently telling you to change professions!)

Reading it reminds me of when I accidentally got my emacs syntax highlighting stuck at “white on white” for a bunch of stuff, but I digress.

I guess I find it to be a completely unproductive, unreadable, and unmaintainable language. It’s OK for throw away prototypes, but I’ve never seen a team throw away a python prototype and rewrite it in another language. I know of python projects that failed and were [repeatedly] rewritten by the same team in python, and I know of python teams that failed and got swapped out for another language, but that doesn’t count. Part of the problem with the python prototyping story is that python wants to own the event loop and asynchrony / thread model, and changing those are often the main reason to swap languages — the next step is always a rewrite in my experience.

Wow. Long rant. One too many ‘assert “” != None’ errors this week. :-)

Re:Some obscure C features by icebraining on Friday January 26, 2018 @02:01AM (#16237490)
Python now supports type annotations, and static checking (using mypy).

Regarding the event loop, I don't think that's true; you can embed libpython and call functions from your own loop: https://docs.python.org/3/extending/embedding.html

Re:Some obscure C features by nickpsecurity on Friday January 26, 2018 @10:59AM (#16240124)
"How is python safer than C?"

Its basic operations don't often crash a system or lead to hacks. It also promotes readable, concise code with a lot of FOSS utilities for things like testing (esp see Hypothesis). This both increases development pace and reduces defect according to about every study that's ever pitted a HLL against C. The best one I've seen in terms of apples to apples was this Ada and C comparison:

http://archive.adaic.com/intro/ada-vs-c/cada_art.pdf

Note that Python isn't the main point or even what I brought up, though. mort's point read like you shouldn't develop C-related tooling in high-level languages like Python. I'm countering saying you should do as much work out of C as you can with even Python being beneficial. I give much better examples in my other response here:

https://news.ycombinator.com/item?id=16240081

Re:Some obscure C features by Const-me on Thursday January 25, 2018 @08:44PM (#16236417)
> Why not?

TLDR: because you need to test the real thing, and using Python to test C code causes too many important differences between tests and production.

To test C code you need to compile it, and there’s no C compiler inside Python. Also if you have C++, sometimes you can abuse templates for similar effects, and Python doesn’t have C++ AST either.

C toolchains largely depend on environment: included & linked files, environment variables, compiler & linker switches, they all make a huge difference. You’ll spend much time replicating that in Python, and it’ll break when you’ll upgrade any part of toolchain (compiler/IDE/build automation/etc).

To a lesser extent, same applies to native binaries as well. If you’ll manage to use Python to produce tests, C compiler to build them, then Python to run them — the runtime environment will be different from the real one.

Re:Some obscure C features by cturner on Friday January 26, 2018 @03:28AM (#16237739)
"To test C code you need to compile it"

I think nickpsecurity has introduced two ideas, and you are only countering the more complex one. The ideas were,

* Use python as a preprocessor

* Use python to call into C.

To the first idea: the language you choose to use as your preprocessor. Instead of using C macros, you could have an alternate DSL that you transform into C. Then you compile this. Given what an awkward thing the C preprocessor is, I am surprised that it continues to hold mindshare against options like this. Awk is a better powerful transform tool, and easily compiled for any platform it is not already on.

Line numbers is a complication if you do your own preprocessor. This post on PHP suggests how to deal with this, https://stackoverflow.com/questions/396644/replacements-for-...

To the other point. The complications you raise are real, but you can manage them away by setting your C project up to create a shared-object build. For example, it is trivial to use Racket to write tests against shared object files (once you know Racket). This still doesn't address the runtime issue you raise.

Separate issue. There is a set of debugging C-preprocessor macros with similar intent to the ones posted in OP published in "Learn C the Hard Way".

Re:Some obscure C features by Const-me on Friday January 26, 2018 @06:34AM (#16238396)
> Use python as a preprocessor

It’ll become harder to find developers, and longer for new ones to start being productive. Also it’ll become harder to do cross platform development because you’ll have to port, and then support, that custom pre-processing tools/DSL for every platform.

> Use python to call into C

Last time I’ve checked Python can’t import C headers. To call into C, you somehow need to negotiate function prototypes & calling conventions between the two languages. Regardless on how you do it manually or automatically, it’s very expensive to support. People do it when the have to (e.g. to interop Python with C libraries), but for tests, the approach is way too expensive for most project.

> it is trivial to use Racket to write tests against shared object files (once you know Racket)

I don’t know Racket but I doubt it’s trivial. SO libraries export C functions, they use C structures for arguments and return values, and these can be very complex. Pointers graph, function pointers, pointers to pointers, variable length structures, other advanced features are pain to use from any other language (besides languages specifically designed to be backward compatible, like C++, objective C, and maybe D). And if you need to test C++ code it’s even harder, unlike C it doesn’t have standardized ABI, i.e. the ABI changes between compilers and their versions.

Re:Some obscure C features by cturner on Friday January 26, 2018 @08:32AM (#16238951)
You introduce a strong point with the issue moving structs over the barrier. When I do it, I create constructor functions in C, and only send pointers over the bridge between the non-C and C. I'm happy to do this, but recognise that it is boilerplatey, and should be seen as a different tradeoff choice rather than a trivial alternative.

Agree regarding C++ ABI also. I don't have much experience with C++. My usual approach is to use C for system calls and bare-minimum surrounds, and then use the wrapping approaches described above to get to a high-level language. This is why the struct issue didn't come to mind for me.

Re:Some obscure C features by nickpsecurity on Friday January 26, 2018 @10:54AM (#16240081)
"* Use python as a preprocessor

* Use python to call into C."

You nailed it. Also note that Python isn't my recommendation so much as what mort brought up that I'm countering with examples showing even it can help. If it was my choice, I'd pick a better HLL for these goals. Let's keep looking at this a bit, though, where I'll introduce those where they're useful.

PHP was a great example I didn't think of on preprocessor. It versus C's is either the 1st or 2nd most used one out there. On the high end, some "term-rewriting languages" can easily handle jobs like refactoring: TXL, Rascal, OMeta, Ohm. Alan Kay et al in STEPS project did a whole OS with graphics stack and all in a mere tens of thousands of lines of code using such a language. One can, as people do with Prolog and STEPS did, pick a general language that can be extended with meta facilities for DSL's or rewriting where opportunistic. Then, where that doesn't work out, you at least have general-purpose language to fall back on.

http://www.vpri.org

(Use Control-F to go to anything that says "STEPS" to find the reports. Work from the bottom up since it's chronological series.)

On your other point, your Racket example is one way it could work. I was also advocating in this thread just using Python itself to build tooling for its own benefits and ecosystem. A lot is already built. Use C for low-level software that strictly needs it if nothing else is available with HLL's like Python for stuff that doesn't need it. The language I've been telling C programmers to check out for scripting-like use is Nim. It looks like Python, has real macros, can call C, and compiles to C. Here's a comparison I just dug up:

https://github.com/nim-lang/Nim/wiki/Nim-for-C-programmers

I also doubt it will be harder to find C developers if tooling is written in HLL's. For one, they just have to use the tooling rather than write it. I doubt most C# developers extend Visual Studio, most Java developers extend NetBeans, and so on. If they do have to learn, using a language like Python or Nim should make low barrier to entry since even folks with no experience pick Python up quickly. A C-like subset of Nim or something similar will be just a lot like C with easier syntax and less stuff to worry about. If anything, productivity will go up over C like shown in about every language study ever done.

I have encouraged people to do C-like languages with safe defaults, cleaner semantics, a REPL, better macros, easy integration of C, and outputting C. That way, one can program in a cleaner language avoiding most headaches and accidents that come with C being designed for 1970's hardware. Aside from Wirth's languages with C syntax, a good picture of what this might look like is Julia language which was femtolisp on the inside. Especially its seemless interoperation with C and Python.

https://julialang.org

Re:Some obscure C features by xtrapolate on Friday January 26, 2018 @05:55AM (#16238270)

  I have been working on Snow, a unit testing library for C.
OP requires a unit testing library for C, so they've written one. What is the alternative solution you propose? The framework itself may appear complex, but the entire end goal here is to provide simple/abstract tools to write tests in a readable/self-documenting manner. The framework itself is the mean, not the end goal.
Re:Some obscure C features by jstimpfle on Thursday January 25, 2018 @05:55PM (#16235526)
My take: rethink the problem and the solution. While there are perfect applications of the preprocessor (such as pasting or stringifying tokens) - yes at some point (rather sooner than later) it gets unmaintainable. But I don't share the sentiment that C wasn't powerful. It has everything one needs (and, due to memory control and cpp, arguably more than most languages).

These days I mostly use C, and occasionally resort to python as an accompanying scripting language to generate data definitions (either in plain text formats or as C structures).

Re:Some obscure C features by nialv7 on Thursday January 25, 2018 @08:28PM (#16236352)
Which more powerful language do you suggest?

Macro metaprogramming is still needed in C++. Other powerful languages might lack some nice properties of C. Like easy interfacing with other languages, and a wide availability.

Re:Some obscure C features by emmelaich on Thursday January 25, 2018 @08:53PM (#16236457)
Considering the parent comment's author he might suggest D :-)
Re:Some obscure C features by akvadrako on Friday January 26, 2018 @03:10AM (#16237674)
Swift
Re:Some obscure C features by faragon on Friday January 26, 2018 @04:25AM (#16237917)
Well, if you use metaprogramming in e.g. 5% of the C code base, it would be OK, in my opinion, as C has lots of benefits vs other high level languages.
Re:Some obscure C features by jacobush on Friday January 26, 2018 @02:04AM (#16237497)
For instance by inventing one. :) Was this how you got started?