源代碼:
int _tmain(int argc, _TCHAR* argv[]) { struct v1 { int a; short b; char c; int d; double e; }; v1* heap_struct = (v1*)malloc(sizeof(v1)); heap_struct->a = 10; heap_struct->b = 20; heap_struct->c = 30; heap_struct->d = 40; heap_struct->e = 50; return 0; }
逆向分析:
結構體中的數據字段是通過名稱訪問的,但編譯器將名稱訪問轉換為數字偏移
所以在反匯編中難以區別
堆分配結構體
push 24 ; Size call ds:__imp__malloc add esp, 4 cmp esi, esp call j___RTC_CheckEsp mov [ebp+heap_struct], eax mov eax, [ebp+heap_struct] mov dword ptr [eax], 10 mov eax, 20 mov ecx, [ebp+heap_struct] mov [ecx+4], ax mov eax, [ebp+heap_struct] mov byte ptr [eax+6], 30 mov eax, [ebp+heap_struct] mov dword ptr [eax+8], 40 mov eax, [ebp+heap_struct] fld ds:__real@4049000000000000 fstp qword ptr [eax+10h]
原類型 大小 偏移 int 4(dword) 0 short 2() 4 char 1(byte) 6 int 4(dword) 8 double 8(qword) 16
為默認4字節對齊
總結:全局和棧分配方式中的結構體 與 普通變量相似 難以區分