看板 C_and_CPP 關於我們 聯絡資訊
Regarding the first question, the answer shall be B0. The virtual keyword suggests the function binding occur in run-time. However the default parameter is static binding and is bound in the compilatio n time. When we compile the program, the parameter a is equal to zero by default. The compiler has no way to know which function gets invoked in the run time. I saw many programmers committing such mistake (note: I use the verb commit in stead of make). It's a disaster. ※ 引述《plsmaop (plsmaop)》之銘言: : ※ 引述《Cosmology (宇宙學型男)》之銘言: : : 2. : : struct test : : { : : int a; : : char b; : : short c; : : double d; : : char e; : : }; : : test這個大小是多少? : : 我可以理解關於記憶體對齊的問題 : : 所以我答總共是24 bytes : : 面試官接著問為什麼是24 以及那他們在系統內的記憶體位置是如何? : : 我回答 : : int 4 : : char 4(對齊後) : : short 4(對齊後) : : double 8 : : char 4(對齊後) : : 所以總共是24 (我的概念可能有錯...) : : 他說24是對的 但是位置的概念不正確 叫我回去測試看看 : : 我回家測試(以下都起始位置) : : a 9435088 : : b 9435092 : : c 9435094 : : d 9435096 : : e 9435104 : : 我理解可能char short那邊可能答錯了 : : 但是即便是這樣 e那個位置對齊以後末三碼應該是108 : : 88到108應該也是20個bytes阿... : : 有前輩可以指點我哪裡概念有錯誤嗎? : : 或是能夠提供方向讓我找資料 謝謝 : https://en.m.wikipedia.org/wiki/Data_structure_alignment : 在Typical alignment of C structs on x86這個條目下有說,會把整個struct湊成其 : 最大的type的大小的倍數 : 如果我沒有理解錯的話,整個struct大小會是8*3=24 : Int 4 : Char 1 ,塞一個1 ,short 2 : Double 8 : Char 1,塞7個 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 107.77.225.225 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1522954023.A.161.html ※ 編輯: wawi2 (107.77.225.225), 04/06/2018 02:49:39
TobyH4cker: +1 for brief explanation. everyone use VF should 04/09 13:03
TobyH4cker: know that. 04/09 13:03