看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《chrisdar (克里斯)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 問題(Question): : 餵入的資料(Input): : 預期的正確結果(Expected Output): : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : #include <iostream> : #pragma pack(4) : #define ALIGNMENT <<<< 這裡該寫甚麼呢 : #pragma pack() : using namespace std; : int main(int argc, char * argv[]) : { : cout << ALIGNMENT << endl; : system("pause"); : return EXIT_SUCCESS; : } : 補充說明(Supplement): : 請不要說直接寫4 wwwwwwww : 因為有的時候 #pragma pack() 的設定遠在天邊 想知道有沒有辦法抓出來 : 補充一下 我只會這個算法 : #include <iostream> : #pragma pack(push) : #pragma pack(4) //1,2,4,8 OK : typedef struct _Test_Pad //but 16 don't work : { : char Reserve; : __int64 Pad; : } Test_Pad; : #define ALIGNMENT (unsigned int)(&(((Test_Pad *) 0)->Pad)) ^^^ 痾 不好意思 ,之前在版上有看過這樣的寫法和解說 但現在都忘了 想再請教一下那個0 是甚麼特別的用法意思 感恩m(_ _)m : #pragma pack(pop) : using namespace std; : int main(int argc, char * argv[]) : { : cout << ALIGNMENT << endl; : system("pause"); : return EXIT_SUCCESS; : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 124.8.128.190
purpose:把記憶體位址0當起點,如果每次對齊4位元組,則0+4這裡 02/18 03:19
purpose:會是 Pad 的起點。 02/18 03:19
VictorTom:不介意的話可以看一下小弟的拙作 #1CH8iM8N 02/18 09:24
VictorTom:推文裡s大也有提到標準裡規定的部份 #18xs7RbK 02/18 09:25
FAITHY:非常感謝樓上兩位大大的回答 感恩:) 02/18 14:32