Skip to content

Instantly share code, notes, and snippets.

@imaami
imaami / Makefile
Last active March 6, 2024 19:52
#template<>
override BIN := test
override SRC := test.c msg.c
override OBJ := $(SRC:%=%.o)
override DEP := $(SRC:%=%.d)
CFLAGS := -O2 -march=native -mtune=native -flto
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
@oconnor663
oconnor663 / example.c
Last active February 27, 2023 03:27
inner/outer RAII in C
#include <errno.h>
#include <stdio.h>
// We want a function that creates a couple files. Here's the naive approach:
int hardcoded_cleanup(const char *path1, const char *path2) {
FILE *file1 = fopen(path1, "w");
if (file1 == NULL) {
return errno;
}

Rust Cheat Sheet

Variables & Mutability

Variables are immutable by default. This makes Rust safer and makes concurrency easier.
Immutable means once a value is bound to that variable, it cannot be changed.
For example:

fn main() {
 let x = 5;
@s9w
s9w / gist:ad9b1dd1ea6fb17e956559c8b352e246
Last active May 29, 2022 17:00
Potential issue with C++20's initialization change

Potential issue with C++20's initialization change

C++20 takes yet another swing at its infamous initialization rules. The players involved this time are Aggregate initialization (type a{1, 2, 3}) and direct initialization (type a(1, 2, 3)). A common pitfall with aggregate init is:

std::vector<int> vec0(5, 9); // 9, 9, 9, 9, 9
std::vector<int> vec1{5, 9}; // 5, 9

So if you don't know what you're doing, {} is potentially dangerous to use with types that might have both "real" constructors and such with std::initializer_list. If you had your head in the sand for 10 11 years and always used () then you never were in danger.

@chadbrewbaker
chadbrewbaker / scribe.md
Last active May 25, 2021 11:09
Daniel Lemire notes
@noseratio
noseratio / adb-clear-packages.ps1
Last active April 17, 2024 00:43
Clear all Android packages and user data via ADB
# Clear all Android packages and user data via ADB, by @noseratio
# Run: powershell -f adb-clear-packages.ps1
# To get ADB: https://community.chocolatey.org/packages/adb
#
# Q: Why not a factory reset?
# A: https://www.reddit.com/r/Android/comments/naetg8/a_quick_powershell_script_for_clearing_user_data/gxtaswl?context=3
$confirmation = Read-Host "This will clear all packages data and user files. Are you sure you want to proceed? (y|n)"
if ($confirmation -ne 'y') {
return
@rr-codes
rr-codes / variadic_param_poc.cpp
Last active May 13, 2020 08:54
A proof of concept C++20 type-safe variadic argument array creation function (using C++20 Concepts and Variadic Templates)
#include <array>
#include <iostream>
template<typename ...Args>
constexpr std::size_t ARG_SIZE() { return sizeof...(Args) + 1; }
template<typename From, typename To>
concept SameAs = std::is_same_v<From, To>;
template<std::size_t N, typename T, SameAs<T>... Ts>
// x64 encoding
enum Reg {
RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI,
R8, R9, R10, R11, R12, R13, R14, R15,
};
enum XmmReg {
XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15,
import Data.Maybe
fizzBuzz n =
let
base divisor s =
if (n `mod` divisor) == 0
then Just s
else Nothing
maybeFizz = base 3 "Fizz"
maybeBuzz = base 5 "Buzz"