Skip to content

Instantly share code, notes, and snippets.

View hazirguo's full-sized avatar

HailinGuo hazirguo

View GitHub Profile
@hazirguo
hazirguo / new_array_initialization.c
Created September 20, 2013 06:29
A very intresting method of array initliazation that I have never used before!
#include <stdio.h>
int main()
{
int array[10] = {
[0 ... 9] = -1,
[8] = 0,
[0] = 45,
[1] = 34,
[5] = 12,
@hazirguo
hazirguo / syscall_int.c
Created September 21, 2013 09:00
inline assembly with gcc to write a syscall function.
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <errno.h>
int main()
{
long rc;
char *file_name = "/etc/passwd";
@hazirguo
hazirguo / find-kth.md
Created August 14, 2014 04:01
两个有序数组任意两个数之和找最小的 k 个数

##问题

给定两个有序的数组 A 和 B,它们任意的两个数之和组成一个包含 N^2 个元素的数组,找出该数组中最小的 k 个数。

##解法

如果直接对这个新的数组进行排序,即使使用较快的快排或堆排序,总的时间复杂度为 N^2 * log(N^2) * k = 2 * k * N^2 * log(N)

那么有什么更高效的解法吗?

@hazirguo
hazirguo / 0_reuse_code.js
Created August 20, 2014 05:03
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@hazirguo
hazirguo / ls.rst
Created August 22, 2014 08:17 — forked from amitsaha/ls.rst

How does ls work?

I wanted to be really able to explain to a fair amount of detail how does the program :command:`ls` actually work right from the moment you type the command name and hit ENTER. What goes on in user space and and in kernel space? This is my attempt and what I have learned so far on Linux (Fedora 19, 3.x kernel).

How does the shell find the location of 'ls' ?

/* fileman.c -- A tiny application which demonstrates how to use the
GNU Readline library. This application interactively allows users
to manipulate files and their modes. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#define SIZE 1024
static int (**hnew())[2] {
return calloc(sizeof(int**), SIZE);
}
static void hdel(int (**e)[2]) {
for (int i = 0; i < SIZE; i++) free(e[i]); free(e);
}
static int (**hget(int (**t)[2], int k))[2] {
for (int h = k & (SIZE - 1); **t && ***t != k; h = ((h + 1) & (SIZE - 1)), t += h);
@hazirguo
hazirguo / linker.md
Created December 28, 2014 07:03
Title

链接器新人指南

这篇文章旨在帮助 C/C++ 程序员理解链接器在背后到底做了什么,在过去的这些年我已经向很多同事解释过同样的问题,所以,我决定是时候将它写下来,以帮助更多的人(我也不用再一一解释了)。

我所帮助的人中让我解释链接器到底干什么这个问题,一个典型的例子是遇到类似下面这样的链接错误:

g++ -o test1 test1a.o test1b.o
test1a.o(.text+0x18): In function `main':
: undefined reference to `findmax(int, int)'

Install Python

$ brew install readline sqlite gdbm
$ brew install python --universal --framework
$ python --version
Python 2.7

Symlinks...