Octave on Guitar

Octave on Guitar

JSON-CC: A C++ JSON Parser/Variant

JSON-CC

The library models JSON object in boost::variant such that grant all flexibility of accessing your parsed objects but without losing performance!

AST Construction with the Universal Tree - boostcon on Blip

C99 offsetof

之前在 ptt 看到的考題,筆記一下 Jserv 在那邊看到的介紹,主要來自 Nigel Jones 的 Learn a new trick with the offsetof() macro

以下是其中一種定義 offsetoff 的方法

#define offsetof(Struct,Member) (size_t) &(((Struct*)0)->Member)

拆解一下

- X: (Struct*)0 把一個 0 轉型成 Struct*
- Y: (X)->Member 對這個 pointer 取他的 Member
- Z: &(Y) 取得這個 Member 的位址

最後 (size_t) Z 轉型成恰當的數值型別,有這個 macro 之後 API 可以做到 “讀取 n byte 放到指定結構的指定成員”,像這樣:

struct S { char a; int i; };
void read_1(struct S* dest, size_t member_offset, size_t nbytes);

用戶端:

struct S dest;
read_1(&dest, offsetof(S, a), sizeof(char));
read_1(&dest, offsetof(S, i), sizeof(int));

缺點是用戶端得多記一個 “S 的 成員 a 型別是 char”;解決方式,支援結構成員的 sizeof

#define sizeof_member(Struct, Member) sizeof( ((Struct*)0)->Member )

用戶端:

// ...
read_1(&dest, offsetof(S,a), sizeof_member(S,a));

注意一下,這邊沒辦法強迫用戶端一定要用 offsetof 跟 sizeof_member ,以 API 設計來說,預期以外的參數還是有可能出現的,比如說

read_1(&dest, 0x12, 4);

所以,好的設計會想要達到 “讀取指定結構的指定成員大小到該結構的該成員”

那麼,加一個額外 macro 幫我們做 sizeof_member 跟 offsetof ,像這樣:

#define ADAPT(API, Dest, Struct, Member) \
    API( \
        DEST, \
        offsetof(Struct, Member), \
        sizeof_member(Struct, Member) \
    )

拿來包 API

#define read_2(Dest, Struct, Member)  ADAPT(read_1, Dest, Struct, Member)

用戶端:

struct S dest;
read_2(&dest, S, a); // 讀 sizeof(a) 放到 dest.a
read_2(&dest, S, i); // 讀 sizeof(i) 放到 dest.i

不過呢,沒有啥方法能避免用戶使用 read_1,需不需要包出 read_2 就見仁見智。

fromscandinaviawithlove:

The Swedish String shelf. See more at String Furniture.

fromscandinaviawithlove:

The Swedish String shelf. See more at String Furniture.

怎樣比較好

企業家串聯支持某個候選人,不管他是否真的傾中,至少他傾向利益。

但是企業家的利益,是國家的利益嗎?是社會的利益嗎?

這個問題對台灣人還是大陸人來說有微妙的差異,共產黨得考量極權的維持,企業若跟黨的利益有所衝突,大概也存活不久;台灣呢?制衡在哪裡?人民?

Johnny Cash - Hurt [HQ]

勢在必行 - 陳勢安&Bii_Eagle

Enable 256 colors in ‘screen’

attrcolor b “.I”

termcapinfo xterm ‘Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm’

defbce “on”

Show that the second smallest of n elements …

9.1-1

Show that the second smallest of n elements can be found with

n + ceil(lg(n)) - 2

comparisons in the worst case.

Within a winner tree, first we generate the smallest number (a winner) which costs (n-1) comparison. After that, to generate the second smallest number, we walk from the winner’s origin leaf to top, such path involves ceil(lg(n)) comparison. Note a sibling to the origin leaf can be selected without comparison since the origin leaf is empty. Thus, we can eliminate 1 comparison from cel(lg(n)) such that obtain a total number of comparisons of

(n -1) + (ceil(lg(n)) -1) = n + ceil(lg(n)) - 2.

Andy Timmons - Falling Down (by MarkleadG)

SDL 開發環境建立

SDL Official Site http://www.libsdl.org/

官網提供了 Visual Studio 2005 (VC8) 的預編譯 binaries,不過在 link 時遇到了一些怪怪的 warning

MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib ‘msvcrt.lib’ conflicts with use of other libs; use /NODEFAULTLIB:library.

不知道哪根筋不對我竟然以為是 VC Runtime 版本的問題 (大概是 conflicts 這個字眼吧)

就開始一連串的苦難…

想要知道結論的請直接往最下面拉。

所以我就去抓了 SDL 的 source 下來自己用 VC9 (2008) 編,source code 裡面有一個 VisualC.zip,解開來裡面就有 VC 的專案檔,注意解開來的目錄大概長這樣

\SDL-1.2.14\VisualC\

解錯了會沒辦法編譯,因為他用相對路徑 (CMake 就沒這問題啦~

打開 .dsw 就直接 build solution 啦 …

當然還是會有問題。

dxsound.h: No such file.

喔~要裝 DirectX SDK 耶 ( XXX 500mb up),其實會用到的是 DXAUDIO 還有 DXINPUT

好吧,裝 … 然後最慘的就是 DirectX SDK 2010 June 他媽的裝不起來啊!!!!

還好 StackOverflow 上面的強者表示

DX 裝到最後要裝 VC 11 的 redist 套件時,發現系統上已經裝了更新的版本,他就會回報錯誤 … 然後你在控制台就找不到反安裝 DX SDK 的選項啦!!!

其實他該裝的都裝了…除了那個 redist ,這麼好的安裝程式,也只有微軟寫得出來,而且就擺著讓它爛一年多,真的很經典。

有趣的是,這篇文章裡面提出的各種可能解法,一點用都沒有,而一個看起來有點像官方的回文(最後一篇) … 真的很官方,不過他介紹了一個小工具 fciv 還不錯用,可以在 windows 下面拿來算檔案的 sha1/md5。

結論:link warning 是因為官網提供的 binaries 都是 release 版本,不是 debug 版本,如果你開的專案是 debug 版本就會出這個 warning,重要嗎? 不重要…。