Live data from Hacker News

How to properly use macros in C

pmihaylov.com

41–50 of 52 posts

Re: How to properly use macros in C

#41

> macros can’t be debugged. When you use a function, that function can be stepped through by the debugger. The macro cannot. Looks like the debugger could use some improvement. > A macro is faster than a function. This is false. Macros may be faster in some cases, but I've seen them being slower . I suspect this is because functions have more type information that can help the optimiser. I've tried it with Monocypher…

> Looks like the debugger could use some improvement.

Try `gcc -g3` (the default is 2). But because C-style macros are context-sensative string operations, the result is not necessarily especially pleasant/useful and can be rather large (slowing down the debugger for everything else).

I find that using `static inline` functions and `static const` variables in the header can just be much nicer to work with all around (some argument type-checking, better stack frame management means alloca is usable, no accidental syntax clashes requiring lots of extra parens to avoid, and real debug info). And, if necessary, use a tiny macro just to form the call to the real function or form the declarations for repeated tabular data. I wouldn't have expected macros to be slower in any case, however.

Re: How to properly use macros in C

#43
post #8

If you're going to write a long lecture to people, it's helpful to do some Googling to fact-check your own understanding. The claim that macros do not exist in other languages is incorrect. Every sensible assembly language compiler has macros to help the programmer with boilerplate. The claim that multiline macros are a problem is correct. Suggesting that a workaround is to use a naming convention for such macros is…

> The claim that macros do not exist in other languages is incorrect

Technically incorrect, but how many of the top 20 languages (https://www.tiobe.com/tiobe-index/) have c style macros? I don't think many of them have a pre-processor at all.

Re: How to properly use macros in C

#44
post #12

The article is an okay start, but it wasn't written by an expert. It lists some real pitfalls but doesn't provide commonly accepted solutions. Some examples... multiple lines: wrap in do { } while (0) - can use a multiline macro like a function call, terminated with ; or ({foo; bar;}) - multiple lines evaluate to 'bar'... Function calls: - in general anything with side effects could be harmful if evaluated more than…

({ }) is handy but is a GCC extension. So is "typeof"; "__typeof__" is the standard spelling.

Which is why C++ has decltype that is standard now.

Re: How to properly use macros in C

#45

Earlier quoted context omitted.

What he means is, you want to use them in a macro. You can't get the effect you probably want by using them in a function. Compare: #define LOG_ERROR() printf("ERROR at %s:%d\n", __FILE__, __LINE__) void logError(void) { printf("ERROR at %s:%d\n", __FILE__, __LINE__); }

Sure, they are pretty useless in a function. But that doesn't mean that the author can go and use incorrect language. :P

Except when they work properly when fully inlined. (Sprinkle GCC/Clang magic here. It is required to make it work.)

Re: How to properly use macros in C

#46

> macros can’t be debugged. When you use a function, that function can be stepped through by the debugger. The macro cannot. Looks like the debugger could use some improvement. > A macro is faster than a function. This is false. Macros may be faster in some cases, but I've seen them being slower . I suspect this is because functions have more type information that can help the optimiser. I've tried it with Monocypher…

> Looks like the debugger could use some improvement. Try `gcc -g3` (the default is 2). But because C-style macros are context-sensative string operations, the result is not necessarily especially pleasant/useful and can be rather large (slowing down the debugger for everything else). I find that using `static inline` functions and `static const` variables in the header can just be much nicer to work with all around…

> I wouldn't have expected macros to be slower in any case, however.

Neither did I to be honest. I was searching for low hanging fruits, and forcing inlining with macros looked made some kind of sense. I expected the compiler to generate the same code, yet somehow it didn't.

Re: How to properly use macros in C

#47

Earlier quoted context omitted.

Sure, they are pretty useless in a function. But that doesn't mean that the author can go and use incorrect language. :P

Except when they work properly when fully inlined. (Sprinkle GCC/Clang magic here. It is required to make it work.)

Is that even possible? It shouldn't be without extensions. (But that seems to be what you're implying anyway.)

Re: How to properly use macros in C

#48
post #9

One strange phenomenon when coding in C is using macros. This is not something which can be seen in other programming languages (other than C++). This is false. Lisp is a language, or rather, a family of languages, where macros play a giant role, and the author does not seem to mention them. But again, Lisps generally have more powerful macros than C does - they allow for arbitrary compile-time computation.

People who know Lisp are unlikely to be writing blogs about using macros in C.

C and C++ blogs generally tend to be of poor quality, because most of the people writing such things are excited newbies who have fewer years of experience than real experts have abstinence.

Re: How to properly use macros in C

#49
post #43
post #8

If you're going to write a long lecture to people, it's helpful to do some Googling to fact-check your own understanding. The claim that macros do not exist in other languages is incorrect. Every sensible assembly language compiler has macros to help the programmer with boilerplate. The claim that multiline macros are a problem is correct. Suggesting that a workaround is to use a naming convention for such macros is…

> The claim that macros do not exist in other languages is incorrect Technically incorrect, but how many of the top 20 languages ( https://www.tiobe.com/tiobe-index/ ) have c style macros? I don't think many of them have a pre-processor at all.

At least 20% of those languages have macros (the three C-derived languages and Assembly). You don't need to have a pre-processor to have macros.

Some of those languages, like Python and C#, have advanced features which eliminate much of the need for macros. Others, like JavaScript and SQL, would be much improved by assembly-language or Lisp style macros.

Re: How to properly use macros in C

#50

Earlier quoted context omitted.

Lisp macros directly manipulate the program's abstract syntax tree (in the case of Lisp, it happens to hava a 1-1 relationship to the source code structure). It's more comparable to a web app's html templater than a syntactic macro system like Lisp's.

It doesn't strictly have a 1-1 relationship because of macros themselves. They're Turing complete, so the same macro can expand into different ASTs based on user input during compilation, presence of any files on the hard drive or even moon phases, so I have to say that this relationship is much more complicated than a simple 1-1. For very simple functions like (lambda (x) (* x x)) this is true, but macros' power gre…

I should have said a read and macroexpanded lisp source has a 1-1 relationship with the AST.

As an aside, the bigger distinction between something like C++ template metaprogramming and Lisp metaprogramming is that while C++ uses a template language completely orthogonal to the runtime language, Lisp macros is nothing more than Lisp code executed at macroexpand-time rather than eval-time.

Post reply on HN