Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
ChunMinChang / makefile
Last active January 15, 2017 05:03
inplace merge sort
CC=g++
CFLAGS=-c -Wall --std=c++11
all: sorting.o
$(CC) sorting_test.cpp sorting.o -o sorting_test
sorting.o: sorting.cpp
$(CC) $(CFLAGS) sorting.cpp
clean:
@ChunMinChang
ChunMinChang / Deadlock.java
Last active January 12, 2017 03:47
Example of deadlock
/**
* Adapted from The Java Tutorial
* Second Edition by Campione, M. and
* Walrath, K.Addison-Wesley 1998
*/
/**
* This is a demonstration of how NOT to write multi-threaded programs.
* It is a program that purposely causes deadlock between two threads that
* are both trying to acquire locks for the same two resources.
@ChunMinChang
ChunMinChang / Makefile
Last active October 24, 2018 21:29
Using reference/pointer instead of copying to get a variable-sized struct object
all:
gcc -o field_offset field_offset.c
./field_offset
# static library:
gcc -c buffer_list.c -o buffer_list.o
ar rcs libbufferlist.a buffer_list.o
# sample in C:
gcc sample.c -L. -lbufferlist -o sample-c
./sample-c
# sample in Rust:
@ChunMinChang
ChunMinChang / alsa_channel_map.c
Last active September 26, 2021 12:03
Set channel map on ALSA
#include <alsa/asoundlib.h> // for ALSA APIs
#include <stddef.h> // for offsetof
#include <stdio.h> // for fprintf
#include <stdlib.h> // for calloc
#include <string.h> // for strcmp
#define DEBUG 1 // Set 1 to log the debugging messages.
#define LOG(...) DEBUG && fprintf(stdout, __VA_ARGS__)
#define ALSA_DEFAULT_DEVICE_NAME "default"
@ChunMinChang
ChunMinChang / copy-pictures.sh
Created March 21, 2017 12:09
Copy all pictures from Android device
#!/bin/bash
# Get ADB Devices
declare -a devices
let i=0
getADBDevices(){
while read line #get devices list
do
if [ -n "$line" ] && [ "`echo $line | awk '{print $2}'`" == "device" ]
then
@ChunMinChang
ChunMinChang / README.md
Last active August 29, 2019 04:46
A counter examples for writing a share library

Duplicate symbol when compiling

We will have duplicate symbol for architecture x86_64 when we compile the programs as follows:

$ g++ -c -Wall bye.cpp   # Generate bye.o
$ g++ -c -Wall hello.cpp # Generate hello.o
$ g++ -Wall main.cpp bye.o hello.o -o run
duplicate symbol __Z3LOGNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE in:
    bye.o
@ChunMinChang
ChunMinChang / AudioStream.cpp
Last active November 26, 2020 04:50
Note for coreaudio
#include <assert.h>
#include <CoreAudio/CoreAudio.h>
#include "AudioStream.h"
#define AU_OUT_BUS 0
// #define AU_IN_BUS 1
AudioStream::AudioStream(Format aFormat,
unsigned int aRate,
@ChunMinChang
ChunMinChang / README.md
Last active April 20, 2017 07:56
Play pthread

pthread

Sample code for demonstrating different behavior in different pthread type

  • PTHREAD_MUTEX_NORMAL
    • This type of mutex does not detect deadlock
    • A thread attempting to relock this mutex without first unlocking it shall deadlock
    • A thread attempting to unlock a mutex locked by a different thread results in undefined behavior
@ChunMinChang
ChunMinChang / README.md
Last active June 8, 2023 14:38
Simulate a C++ class in C for linked-list implementation

Class in C for linked-list implementation

An example to simulate a C++ class in C.

This is a comparison to [ChunMinChang/8e04130e778d77e0b30b8954cc5f2473][c++], which implement a linked-list in C++.

TODO

  • Implement delete function to remove a node from the list
  • Implement move function to move cursor to a specific node
  • Implement search function to return one specific node
@ChunMinChang
ChunMinChang / README.md
Last active May 4, 2017 08:25
Linked list in C++

Linked-list in C++

This is a comparison to [ChunMinChang/31f11789bb859356daf05107e8fc859e][class_in_c], which simulates C++ classes to implement linked-list in pure C.

TODO

  • Implement delete function to remove a node from the list
  • Implement move function to move cursor to a specific node
  • Implement search function to return one specific node

Comparison with [C version][class_in_c]