March 18,2009
少見的C記憶體管理function
calloc (count, size)
大致上用法和malloc差不多,不過它是用來方便allocate structure的array
要傳的參數是兩個,第一個參數是structure個數,第二個則是structure的大小
另外用這個function allocate完的記憶體是清除過的
offsetof (struct name, struct member)
這個函數超詭異的,用來算出struct中某個變數的位置……
在下面這個Quicktime的範例程式中第一次看見這兩個function的組合
主要用來 allocate 最後一個欄位是可變動長度structure array的structure
基本上這兩個function組合起來,大致上只是節省了一些programmer寫算式的時間
但是看起來就是比較帥(誤)
大致上用法和malloc差不多,不過它是用來方便allocate structure的array
要傳的參數是兩個,第一個參數是structure個數,第二個則是structure的大小
另外用這個function allocate完的記憶體是清除過的
offsetof (struct name, struct member)
這個函數超詭異的,用來算出struct中某個變數的位置……
在下面這個Quicktime的範例程式中第一次看見這兩個function的組合
主要用來 allocate 最後一個欄位是可變動長度structure array的structure
基本上這兩個function組合起來,大致上只是節省了一些programmer寫算式的時間
但是看起來就是比較帥(誤)
| AudioBufferList * buflist = calloc(1, offsetof(AudioBufferList, mBuffers[numchannels])); |
引用URL
http://cgi.blog.roodo.com/trackback/8537093