18
votes
Macro for allocation in C
p = malloc(n*sizeof *p);
This is dangerous if n gets large, because the multiplication could overflow. After the overflow, too little memory has been allocated but ...
17
votes
Accepted
A simple error messaging and logging system via macro(s) in C++
You’ve already noted that this could be done better without macros, so I won’t belabour the point. I will note, though, that your goal—“to refresh [your] skills at writing good solid macros”—makes ...
8
votes
Macro for allocation in C
You've taken an expression statement and unnecessarily made a do/while statement out of it. You need parentheses around your macro parameters. You don't need to ...
8
votes
Accepted
Calendar for any given month/year
If you have a C++ standards-compliant compiler, then you should not need to use <conio.h> under Windows (_kbhit and ...
8
votes
JVM bytecode instruction struct with serializer & parser
std::variant vs union
Avoid blatant memory waste: One of the most common ways i saw other projects implement their instruction ...
7
votes
The perfect function alias
Is there a situation where the compiler will not optimize away the wrapper because the wrapper and the original function have different semantics?
Yes to the first part and no to the second. Any ...
7
votes
Accepted
Macro to generate an enum and an array of strings
Most macro solutions are not readable nor maintainable. You should avoid macro solutions like this.
The best way is to not use macros at all:
...
7
votes
C++ (Cross-Platform) Predefined Library
Design review
There are other libraries that do more or less what you’re trying to do, in whole or in part (for example Boost.Config). Even so, I’m skeptical about why most of this stuff would be ...
6
votes
Calendar for any given month/year
I found the basic model of how the program works more annoying than useful. I'd expect typing just "cal" to pick a reasonable default, such as displaying the calendar for the current month (and ...
6
votes
Accepted
Function declarations using macros to support multiple platforms
Keep the DLLExport macro on all platforms, but make the definition resolve to nothing unless you're on Windows. You might also want to rename the macro to something more neutral. Something like this:
...
6
votes
JVM bytecode instruction struct with serializer & parser
Some style critique from me (the principles seem sound).
Since the standard headers are independent of each other, we can include them in a consistent order. I prefer alphabetical, but you could ...
5
votes
Macros for bitsets / bit-flags in C
Most severe issue:
Never invent secret macro languages! This is about the single-worst thing a C programmer can ever do, all categories.
You are perfectly free to assume that any C programmer will ...
5
votes
Accepted
Macros for bitsets / bit-flags in C
Small portability bug: if we're using false in the macros, then bflags.h should include ...
5
votes
Index Match implementation
If you want to make the code "easier to read and look neater", the first thing that I would do is to run it through an indenter. This is currently haphazard at best, and makes it difficult to follow.
...
5
votes
Accepted
Format log with a small macro logger
Looking at the code, it is clear that you took a part from existing C code.
Overal the code is acceptable, although when I should accept it, I would recommend some changes.
As already indicated in ...
5
votes
Clang preprocessor concatenates an extra space vs. gcc - standard C99
I think this is beyond the jurisdiction of the C standard. See https://stackoverflow.com/questions/37796947/spaces-inserted-by-the-c-preprocessor. All the standard says on the topic is a footnote:
...
5
votes
Macro to generate an enum and an array of strings
There's a variant style of creating X-Macros that I much prefer to the example presented by Lundin (in his otherwise excellent answer).
Rather than use the name X ...
5
votes
A simple error messaging and logging system via macro(s) in C++
Observation
This is a common one beginners do. And to be blunt I wish they would not do this. It would be much better to learn how to sue the system logging tools.
Questions
Are there any apparent ...
5
votes
Populate a color table in XCB
For me it looks like you create some kind of color table here.
In the first step I would make the function more flexible by giving the colors as parameter
...
5
votes
Accepted
Pseudo-Generic Array Stack in C
A better than usual implementation.
Is it a good idea to implement such a data structure using macros?
It is tricky to do well. User code looks like it is using non-macro code, yet the usual ...
5
votes
C Macro Generic Linked List
[Just a short review]
Not a 3-way compare
data->size - *key is never negative as it is a wide unsigned subtraction and so does not meet "Requires a 3-way ...
5
votes
Accepted
C Macro Generic Linked List
General Observations
Very interesting code and question.
This looks like an attempt to write object oriented code in C. I've tried to do this myself, it has inherent problems. The use of ...
5
votes
Accepted
C23 Vector Macro Implementation
Improve macro safety
You seem to have hit the usual pitfalls of macros - remember these are text substitutions done by the C preprocessor, so we need to ensure that the the rules of precedence don't ...
5
votes
Accepted
Custom derive macro to create getters and setters
The code uses both proc_macro and proc_macro2, is this necessary?
This is normal. proc_macro...
4
votes
Accepted
Fastest FIFO with macros for use embedded devices
Since you've mentioned that this code is meant to be used in ISRs, I have to notice an absence of synchronization.
_fff_write_safe should inform the caller on ...
4
votes
Macro for allocation in C
There are a couple questionable things here, first since ALLOC() is a macro you don't need to pass the address of p. If ...
4
votes
The perfect function alias
This function alias is deficient. decltype(auto) will get you the right return type, but it is not SFINAE friendly. For example, the following fails to compile:
<...
4
votes
Time axis with working days, weeks numbers, month-year
There are a few things you can do in your coding to improve both the logic and the organization of your application. Your code does work and kudos for already using a memory-based array to speed up ...
4
votes
Accepted
C++17 enum macro with to_string operator
I’m really not a fan of this idea, on a lot of levels. First of all, it’s extremely limited; it only works for the most basic enum definitions. As soon as you add ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
macros × 122c × 40
c++ × 39
vba × 10
excel × 10
performance × 7
library × 6
variadic × 6
c++11 × 5
error-handling × 5
vectors × 5
enum × 5
common-lisp × 5
racket × 5
beginner × 4
unit-testing × 4
comparative-review × 4
rust × 4
c++17 × 4
stack × 4
objective-c × 4
collections × 4
clojure × 4
strings × 3
array × 3