Skip to content

Instantly share code, notes, and snippets.

@Hadaward
Hadaward / list.c
Last active March 7, 2025 00:53
Curtain Collections List
#include "list.h"
List list_instance(size_t type_size) {
return (List){
.data = malloc(0),
.size = 0,
.type_size = type_size
};
}
@m1lkweed
m1lkweed / for_else.c
Last active July 15, 2025 18:42
Porting python's `for`/`else` to C with no `setjmp`.
#include <stdio.h>
#define py_for(...) _Pragma("GCC diagnostic push")_Pragma("GCC diagnostic ignored \"-Wdangling-else\"") \
for(struct{int count; bool broke; bool once; bool copy;}py_for = {}; \
(py_for.count < 2) && !py_for.broke; ++py_for.count)if(!py_for.count)for(__VA_ARGS__) \
if(py_for.broke){break;}else for(py_for.once = true; (py_for.copy = py_for.broke), \
(py_for.broke = py_for.once), py_for.once && !py_for.copy; py_for.broke = py_for.once = false) \
_Pragma("GCC diagnostic pop")
#define py_whl(...) py_for(;__VA_ARGS__;)
@Eczbek
Eczbek / resources.md
Last active August 24, 2025 03:10
C++ resources and a rearrangement of `learncpp.com`
@eisenwave
eisenwave / pointers.md
Last active July 25, 2025 09:13
How pointers in C++ actually work

How pointers in C++ actually work

Abstract: This document teaches you from the very basics to advanced features such as std::launder how pointers in C++ work. It is aimed at developers of any skill level. However, it links to the C++ standard so that advanced readers can verify the information and investigate further.

Motivation: Most tutorials on pointers are aimed at beginners, and present a simplified model. Some tutorials perpetuate an explanation of pointers that is solely based on their implementation (pointer = memory address). This tutorial aims to provide a comprehensive explanation of pointers that is in line with how they actually work from a language perspective.

@m1lkweed
m1lkweed / un_builtin.c
Last active January 17, 2025 12:56
Replacements for various gcc builtins using c23's `typeof` and c11's `_Generic`
// Compares types of both parameters. Returns true if the types are compatible, otherwise false.
// Parameter y may not be a variably-modified type.
// Designed to be a drop-in replacement for gcc's __builtin_types_compatible_p
#define types_compatible_p(x, y) _Generic(((typeof_unqual(x)*){}), typeof_unqual(y)*:1, default:0)
// Cast the bits of arg to type. Parameter type must be a type and must not be a literal or object with that type.
// Designed to be compatible with g++'s __builtin_bit_cast
#define bit_cast(type, ...) ( \
union{typeof(__VA_ARGS__) in; typeof(type) out; \
int enforce_type:_Generic((int(*)(int(type)))0, \
@eisenwave
eisenwave / case_against_almost_always_auto.md
Last active September 19, 2025 09:13
The case against Almost Always `auto` (AAA)

The case against Almost Always auto (AAA)

Introduction

I've been writing C++ for half a decade now, and auto has always been a great source of discomfort to me. Whenever I came back to a past project that makes extensive use of it, I found myself confused, and first had to look at all the types before I could make sense of it.

Similarly, I've worked as an intern at a company that had a AAA policy for its code base. Whenever something didn't work, and I had to debug some code, half the time was spent just looking up types.

@eisenwave
eisenwave / cpp-memory.md
Created August 2, 2023 16:39
The C++ Memory Model and Multithreading - According to the Standard

The C++ Memory Model and Multithreading
According to the Standard

All code you write is ultimately designed to read and write memory. One aspect of this is the memory model of the language; something which is accompanying us as developers, but we rarely have to think about.

How come, for example, the following code does what we expect it to?

int x = 1;
x = 5;
@eisenwave
eisenwave / cpp-keywords.md
Created July 20, 2023 11:46
The Effect of `inline`, `static`, etc. on Symbols in C++

The Effect of inline, static, etc. on Symbols in C++

inline

  • Free function: Makes it an inline function. Linkage is unchanged, and external by default.
  • Static member function: Makes it an inline function. Linkage is the same as that of the class. inline is redundant for inline definitions, except when modules are used. In that case, inline would make the function part of the module interface as usual.
  • Non-static member functions: Makes it an inline function. The same rules for linkag and modules apply.
@STCollier
STCollier / main.c
Created May 2, 2023 00:58
Golfed code that converts a 10 digit number to a phone number. e.g., 1234567890 -> (123) 456-7890
main(){char s[10];scanf("%[^\n]",s);printf("(%.*s) %.*s-%.*s",3,s,3,s+3,4,s+6);}
@eisenwave
eisenwave / divison_proposal.md
Last active May 27, 2025 20:07
C++26 Proposal Draft - <intdiv> Header for Integer Divisions

<intdiv> Header for Integer Divisions

Introduction

C++ currently offers only truncating integer division. As a consequence, the remainder operator's sign is same as the sign of the dividend. Alternative rounding modes and remainder sign behaviour are useful.

This proposal adds an <intdiv> header containing free functions for obtaining the quotient and remainder for different rounding modes, such as rounding towards the nearest integer, towards the infinities, etc.