Skip to content

Instantly share code, notes, and snippets.

View scvalex's full-sized avatar

Alexandru Scvorțov scvalex

View GitHub Profile
@scvalex
scvalex / fork.c
Created May 11, 2013 01:39
A useful test for people who claim to know C: What does this program print? The answer requires a basic understanding of `stdio` and the semantics of forking. The answer is in the comments.
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
printf("Hello, ");
fork();
printf(" World!\n");
return 0;
}
@scvalex
scvalex / subclass.cpp
Created May 11, 2013 01:37
Subclassing in C++: overriden fields are not removed, the syntax for accessing them is just unusual. This is not a surprise, since subclassing strictly extends objects.
#include <cstdio>
using namespace std;
class A {
public:
int fa;
};
class B : public A {
@scvalex
scvalex / mi.cpp
Last active December 16, 2015 21:59
Fun with multiple inheritance and pointers in C++
#include <cstdio>
#include <inttypes.h>
using namespace std;
class Left {
int32_t a, b;
};
class Right {
@scvalex
scvalex / quine.sh
Last active November 30, 2019 16:08
A real quine in Bash
#!/bin/bash
T='#!/bin/bash
Q=$(printf "\x27") # A literal single quote
echo "$T" | head -n2 -
echo T=$Q"$T"$Q
echo "$T" | tail -n6 -'
@scvalex
scvalex / quine.sh
Created April 11, 2013 01:14
Not really a quine in sh
#!/bin/sh
cat "$0"
@scvalex
scvalex / fixed_frequency_loop.c
Last active December 14, 2015 13:49
A loop that executes an action a certain number of times per second.
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
/* How many times per second do we want the code to run? */
const int frequency = 2;
/* Pretend to do something useful. */
void do_work() {
@scvalex
scvalex / phil.c
Created December 18, 2012 14:22
Starving philosophers
#include <pthread.h>
#include <stdio.h>
void philosopher1(pthread_mutex_t chopsticks[]) {
pthread_mutex_lock(&chopsticks[0]);
pthread_mutex_lock(&chopsticks[1]);
printf("Philosopher 1 is full.\n");
pthread_mutex_unlock(&chopsticks[0]);
pthread_mutex_unlock(&chopsticks[1]);
}
@scvalex
scvalex / fib.c
Created December 18, 2012 13:25
A segfaulting program to calculate Fibonacci numbers.
#include <stdio.h>
int fib(int n) {
int f1 = fib(n - 1);
int f2 = fib(n - 2);
if (n <= 2)
return 1;
return f1 + f2;
}
@scvalex
scvalex / receiver.hs
Created November 11, 2012 23:37
Test the effective speed of a network link
module Main where
import Control.Concurrent
import Control.Exception
import Control.Monad
import qualified Data.ByteString as BS
import Network.Socket hiding ( recv )
import Network.Socket.ByteString ( recv )
import Text.Printf ( printf )
@scvalex
scvalex / classesToDot.sh
Created November 4, 2012 16:36
Made a Graphviz Dot file of class dependencies inside a package
#!/bin/bash
set -e
classes_full=$(find . -name '*.java')
classes=""
for c in ${classes_full}; do
classes="${classes} $(basename $c .java)"
done