Skip to content

Instantly share code, notes, and snippets.

@pstef
pstef / gist:4726614
Last active December 12, 2015 06:08
memcpy(d, s, n)[n - 1] = 0;
memmove(d, s, n)[n - 1] = 0;
@pstef
pstef / gist:4750224
Created February 10, 2013 17:02
<zid`> There's a really cryptic gcc command you can invoke to tell you all the #defines that are in a blank C file
gcc -dM -E - < /dev/null
@pstef
pstef / gist:4996382
Created February 20, 2013 15:32
From ##C: number of decimal digits an unsigned integer type can have at most
#include <stdio.h>
#include <limits.h>
/* number of decimal digits an unsigned integer type can have at most,
* not including the terminating 0 byte of strings.
* One needs log_10(2) digits per bit, 28/93 is slightly above this value.
* If bits * 28 is not divisible by 93, the result is truncated and one byte
* short, hence add 1.
*
* E.g. one needs 32 * l(2)/l(10) ~ 9.6323 ~ 10 bytes for 32 bit unsigned int.
@pstef
pstef / gist:5072515
Last active December 14, 2015 10:29
GNU indent: what exactly -gnu is
-gnu - Use GNU coding style. This is the default.
----------------------------------------------------------------------------------------------
-nbad - Do not force blank lines after declarations.
-bap - Force blank lines after procedure bodies.
-nbc - Do not force newlines after commas in declarations.
-bbo - Prefer to break long lines before boolean operators.
-bl - Put braces on line after if, etc.
-bli2 - Indent braces 2 spaces.
-bls - Put braces on the line after struct declaration lines.
-ncdb - Do not put comment delimiters on blank lines.
@pstef
pstef / gist:5161793
Last active December 14, 2015 22:58
Books to read and re-read.
Kernighan and Ritchie: The C Programming Language
Stephen Prata: C Primer Plus
Brian Hook: Write portable code
Andrew Koenig: C traps and pitfalls
Peter v d Linden: Expert C programming - Deep secrets
C Unleashed
Jon Bentley: Programming Pearls
Robert Sedgewick: Algorithms in C
@pstef
pstef / gist:5189951
Created March 18, 2013 19:13
Toggle Valgrind data collection on entry/exit of function
valgrind --tool=callgrind --toggle-collect=function ./program
@pstef
pstef / enum.c
Created March 21, 2013 13:34
Why I didn't like enum types in C until I learned about clang -Weverything -fsanitize=enum
#include <string.h>
#include <stdio.h>
enum car { honda };
enum number { ZERO, ONE };
enum number function_returning_number(char const *keyword) {
if (!strcmp(keyword, "zero"))
return ZERO;
@pstef
pstef / last-components.sql
Last active August 29, 2015 13:56
<lluad> Is there a tidier way to get the last two components of a hostname than (regexp_matches(foo, '\.([^.]+\.[^.]+)$'))[1] ? It works, but kinda offends even me. It's only going to be used on hostnames, so I don't really care what it does to non-hostname input.
SELECT array_to_string(a[array_upper(a, 1)-1:array_upper(a, 1)], '.')
FROM string_to_array('gist.github.com', '.') a
psql -d 'linspector' <<EOF
DROP TABLE IF EXISTS symbols, "files";
CREATE TABLE symbols (
filename text NOT NULL,
flag "char" NOT NULL,
symbol text NOT NULL,
PRIMARY KEY (filename, flag, symbol)
);
COPY symbols (filename, flag, symbol) FROM stdin;
@pstef
pstef / Postgres-json-output.txt
Last active October 21, 2018 07:58
Showing of Postgres's features
WITH
manufacturers (name, headquarters) AS (VALUES
('BMW', 'Munich'), ('Toyota', 'Toyota'), ('Fiat', 'Turin')
),
models (manufacturer, name, year) AS (VALUES
('BMW', 'm4', 2000), ('BMW', 'm5', 2000),
('Toyota', 'corolla', 2000), ('Toyota', 'yaris', 2000),
('Fiat', 'panda', 2000), ('Fiat', 'uno', 2000)
),
products (manufacturer, model, color, quantity, pretax) AS (VALUES