Skip to main content
10 votes

int128 handling in c-code, gcc / glibc / linux

Avoid naked magic numbers Why 45 in char str[ 45 ];? Instead ...
chux's user avatar
  • 36.4k
9 votes
Accepted

Integer to string via gcc converter

minus sign void print_integer(int i_val) You accept negative quantities, but there's no logic for outputting a minus sign. Consider making the parameter unsigned. ...
J_H's user avatar
  • 42.3k
8 votes

Integer to string converter - gcc builtins only

Make isBigEndian() constexpr If you can use C++20, then I'd just use std::endian to detect ...
G. Sliepen's user avatar
  • 69.3k
7 votes

int128 handling in c-code, gcc / glibc / linux

Trailing whitespace on lines Please remove the trailing space at the end of each line before the newline. Some code editors will automatically do this for you. Unindent global variables ...
CPlus's user avatar
  • 1,395
6 votes

Safer & simpler allocation functions and macros

Portability While #pragma once is widely supported it is not part of the C programming standard. To make this library more portable, use include guards such as ...
pacmaninbw's user avatar
  • 26.1k
6 votes

128-bit integer type in GCC/Clang

Consider the future when uint128_t is standard and available You don't want your code to stomp on standard names, yet not require much code change. Rather than <...
chux's user avatar
  • 36.4k
6 votes
Accepted

Consolidating GNU C's and C23's attributes

For any attribute that has either a standard version or a GCC/Clang extension, the separate #if clauses need to be #if/...
Davislor's user avatar
  • 9,115
5 votes

4 different implementations of modulo with fully defined behavior

The code may be free of undefined behavior, but it relies on implementation-defined behavior because of the exit(-1). Read its definition in the C standard, not in ...
Roland Illig's user avatar
  • 21.9k
5 votes
Accepted

4 different implementations of modulo with fully defined behavior

Is there an error somewhere, something that i missed? Is there an error somewhere, something that i missed? Certainly have most of main street covered. ...
chux's user avatar
  • 36.4k
5 votes

Can you please give feedback on my C++ Code?

Don't optimize prematurely This Code uses the cstdio include for good performance If you write C++ code, it is better to use the C++ way of doing things. While ...
G. Sliepen's user avatar
  • 69.3k
5 votes
Accepted

Safer & simpler allocation functions and macros

Use Doxygen to document your code It's already mentioned by others, but you have a lot of comments. It's good to document all the functions, but you are not using a standard code documentation ...
G. Sliepen's user avatar
  • 69.3k
5 votes
Accepted

Efficiently read a file into a (C) string using POSIX APIs

This is a good project, one that could be very useful in practice. I’ve had two or three C++ blogs over the years, and every time I spin up a new one, I include an updated version of a very old post I ...
indi's user avatar
  • 16.5k
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 for counting number of elements in an array

We need to protect a when it's an expression - we've put parens around a[0] where we should have put them around ...
Toby Speight's user avatar
  • 88.4k
5 votes

Register "%b" conversion specifier

EDIT: I also fixed a typo: main should be int main(void)! After fixing the bugs @chux found, there was still another bug: This ...
alx - recommends codidact's user avatar
5 votes

int128 handling in c-code, gcc / glibc / linux

I have some comments about the comments in your code. Boilerpate I would prefer not to see the boilerplate copyright comments at the top of the file, having to scroll past a half of a screen to get to ...
toolic's user avatar
  • 15.9k
4 votes
Accepted

Register "%b" conversion specifier

zero min_len is calculated as 0 when val == 0. I'd expect min_len to be 1. Casual ...
chux's user avatar
  • 36.4k
4 votes
Accepted

Bash script - compile and run c++ code for coding competitions

#!/bin/bash I don't see why you're using Bash for this - there should be no problem using standard POSIX shell. ...
Toby Speight's user avatar
  • 88.4k
4 votes

C++ Template to implement the Factory Pattern

Seems pretty reasonable. I mean, I definitely wouldn't put this in production code because it relies on parsing a class name out of __PRETTY_FUNCTION__, and that's ...
Quuxplusone's user avatar
  • 19.7k
3 votes
Accepted

Reference-counted smart pointer in C

Tolerate NULL during clean-up As free(NULL) is OK, consider a NULL test for clean-up ...
chux's user avatar
  • 36.4k
3 votes

4 different implementations of modulo with fully defined behavior

Is there an error somewhere, ... Like a set of inputs that don't give the right result. Code lacks basic tests to answer that. Sample test harness. (It lacks % 0 ...
chux's user avatar
  • 36.4k
3 votes

A little C++ program to have timed rounds for a game

Make use of the do/while loop (instead of a while loop) where you can. Both ...
1201ProgramAlarm's user avatar
3 votes

A little C++ program to have timed rounds for a game

First, it's really not too bad for a beginner, there are functions with local variables and the function names are descriptive. I'm impressed with the fact that all ...
pacmaninbw's user avatar
  • 26.1k
3 votes

Encapsulating snprintf to simplify usage: sbprintf & swnprintf

Welcome to the world of modern C++! The OP has stated that "I don't think you need to write code that a C programmer couldn't understand to say that a program is C++." That's partially right — ...
L. F.'s user avatar
  • 9,705
3 votes
Accepted

Encapsulating snprintf to simplify usage: sbprintf & swnprintf

<cstddef> defines std::ptrdiff_t - the implementation is allowed to also define it in the global namespace, but it's not ...
Toby Speight's user avatar
  • 88.4k
3 votes

Encapsulating snprintf to avoid repetition of sizeof

No need to resort to non-portable GNU C extensions: 1. Replacement for GNU C's __builtin_types_compatible_p(): The intent I believe is to differentiate between ...
Madagascar's user avatar
  • 10.1k
3 votes

Encapsulating snprintf to avoid repetition of sizeof

Pedantically, if (snprintf(buff, sizeof(buff), format, ...) >= SSIZEOF(buff)) goto err; is an insufficient test. Test for ...
chux's user avatar
  • 36.4k

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