Skip to main content
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 ...
jpa's user avatar
  • 391
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 ...
indi's user avatar
  • 16.5k
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 ...
1201ProgramAlarm's user avatar
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 ...
Cris Luengo's user avatar
  • 7,021
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 ...
G. Sliepen's user avatar
  • 69.3k
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 ...
Rakete1111's user avatar
  • 2,582
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: ...
Lundin's user avatar
  • 4,919
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 ...
indi's user avatar
  • 16.5k
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 ...
Jerry Coffin's user avatar
  • 34.1k
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: ...
rjnilsson's user avatar
  • 1,641
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 ...
Toby Speight's user avatar
  • 88.3k
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 ...
Lundin's user avatar
  • 4,919
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 ...
Toby Speight's user avatar
  • 88.3k
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. ...
Comintern's user avatar
  • 4,242
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 ...
JVApen's user avatar
  • 1,191
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: ...
sudo rm -rf slash's user avatar
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 ...
luser droog's user avatar
  • 2,138
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 ...
Loki Astari's user avatar
  • 97.7k
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 ...
Thallius's user avatar
  • 221
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 ...
chux's user avatar
  • 36.4k
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 ...
chux's user avatar
  • 36.4k
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 ...
pacmaninbw's user avatar
  • 26.1k
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 ...
Toby Speight's user avatar
  • 88.3k
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...
Kevin Reid's user avatar
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 ...
vnp's user avatar
  • 58.7k
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 ...
pacmaninbw's user avatar
  • 26.1k
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: <...
Justin's user avatar
  • 3,333
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 ...
PeterT's user avatar
  • 2,166
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 ...
indi's user avatar
  • 16.5k

Only top scored, non community-wiki answers of a minimum length are eligible