Skip to content

Instantly share code, notes, and snippets.

@aprell
aprell / cancel.c
Created June 14, 2013 15:15
Sending a cancellation request with pthread_cancel
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
static pthread_t threads[2];
static int IDs[2] = { 1, 2 };
static int running;
static int waste_cycles(int n)
@aprell
aprell / scanset.c
Created July 3, 2013 18:42
Scansets in input format strings
#include <stdio.h>
typedef struct person {
char firstname[20];
char lastname[20];
unsigned int age;
} Person;
int main(void)
{
@aprell
aprell / strconst.c
Created July 4, 2013 14:21
Storage for string constants
#include <stdio.h>
int main(void)
{
char a[] = "Always writable";
char *b = "Likely not writable";
const char c[] = "Never writable";
char *x = "xyz";
char *y = "xyz";
@aprell
aprell / const.c
Last active December 19, 2015 08:58
Constant pointers and pointees
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
const int c = 3;
int *pa = &a;
const int *pb = &b;
@aprell
aprell / sieve.c
Created July 10, 2013 18:27
Prime sieve using queues
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "queue.h"
static int *numbers;
Queue *generate(int n)
{
Queue *input = queue_new();
@aprell
aprell / fucpp.c
Created July 26, 2013 18:07
Reminder: Never ever make this mistake again.
#include <stdio.h>
#define A baz
int main(void)
{
#if A == foo || A == bar
printf("Wrooong!\n");
#endif
return 0;
@aprell
aprell / cleanup.c
Created July 27, 2013 16:10
Running a function when a variable goes out of scope (GCC, Clang)
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#define refcount __attribute__((cleanup(release)))
#define REF_COUNT refcount_t _rc
typedef int refcount_t;
@aprell
aprell / random.sh
Created August 11, 2013 15:59
Bash's $RANDOM
a=foo.bar; echo ${a%%.*}_$RANDOM.${a#*.}
@aprell
aprell / calc.sh
Created August 11, 2013 16:08
Lua as a calculator
y=1+2+3+4+5; lua -e "print(($y)/2)"
@aprell
aprell / temp.sh
Created August 11, 2013 17:22
Use process substitution instead of temporary files where possible
./interpreter <(tr -s " " "\n" < $1)