Skip to content

Instantly share code, notes, and snippets.

View hpcx82's full-sized avatar

hpcx hpcx82

  • Shanghai
View GitHub Profile
@hpcx82
hpcx82 / initarray.cpp
Created July 18, 2012 08:36
You can only initialize array on declaration for stack variables
#include <iostream>
using namespace std;
int main()
{
// initialize stack array variable
int x[] = {1, 3, 4};
int b[][2] = {{1, 2}, {3, 4}};
@hpcx82
hpcx82 / result-g++.txt
Created July 16, 2012 02:22
Test the vptr position in object model
sizeof(Base)=16
sizeof(Derived)=16
0x7fffd1303400
0x7fffd1303408
vptr is at the beginning? true
@hpcx82
hpcx82 / HashCodeGenerator .java
Created July 11, 2012 06:32
generate hashcode based on effective java
package baiyanhuang;
import java.util.Arrays;
/**
*
* A utility class to help generate hash code: This is based on the theory from Effective Java, Item 9
*
*/
public class HashCodeGenerator {
@hpcx82
hpcx82 / constructor_seq.cpp
Created July 10, 2012 12:42
test the object construction sequence
#include <iostream>
using namespace std;
class Base
{
public:
Base() {cout << "Base constructor called" << endl;}
};
@hpcx82
hpcx82 / singleton.cpp
Created July 1, 2012 09:31
Spin lock implementation in Windows and depends on it a double-checked singleton.
// we use double-lock check in singleton to solve the problem we met in spinlock1.cpp, but hey, that itself is a lock, it has to use some atomic intrusion/memory barrier to solve this synchronization problem
template<class T>
class SingletonHolder
{
private:
static volatile T* instance;
static volatile int m_lockObj;
SingletonHolder() {}
@hpcx82
hpcx82 / mutex-test.cpp
Created July 1, 2012 08:35
spin lock with 9 threads waiting - who is next is random/mutex with 9 threads waiting, the first one in the queue will be the next
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include<iostream>
using namespace std;
static pthread_mutex_t foo_mutex;
void* get_maokeng(void* param)
{
@hpcx82
hpcx82 / d3testing.html
Created June 29, 2012 02:13
test d3 framework.
<html>
<!-- http://d3js.org/ -->
<head>
<title> D3 Testing </title>
<script src="http://d3js.org/d3.v2.js"></script>
<style type="text/css">
body {
background-color: green;
color: white;
}
@hpcx82
hpcx82 / array_size.h
Created June 17, 2012 03:13
A macro to get array size
#ifndef _LZ_UTILITY_COMMON_H
#define _LZ_UTILITY_COMMON_H
// Array size
template <typename T, size_t N>
char ( &_ArraySizeHelper( T (&array)[N] ) ) [N];
#define ARRAY_SIZE(array) ( sizeof( _ArraySizeHelper(array) ) )
#endif
@hpcx82
hpcx82 / CleanSource.bat
Created June 17, 2012 02:24
remove all binary files in source folder
echo Clear Mir Baiyan's coding arena!!!
cd ..
del /f /s /q *.obj
del /f /s /q *.pdb
del /f /s /q *.ilk
del /f /s /q *.pch
del /f /s /q *.ib_tag
del /f /s /q *.idb
del /f /s /q *.exe
del /f /s /q *.ncb
@hpcx82
hpcx82 / linked_list.cpp
Created June 17, 2012 02:17
#algorithm# quick sort by linked list
#include "linked_list.h"
#include "timer.h"
#include "LazyTest.h"
#include <list>
// standard
#include <ctime>
#include <random>
#include <algorithm>