马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kangels 于 2013-3-19 10:23 编辑 9 v7 J. x0 \1 Y) F, g2 k V+ P
6 D* g; _# U6 ~. n' j, I
丢失的类型信息7 O7 p P( |! j x/ l1 c, y8 ]- \! y
猜测
% @: K3 z r+ x* C" k指针变量应该存储两方面的信息:地址,类型信息
0 t$ [ P" R1 g$ N3 U" n' {4 c1 K4 l4 h8 h; m: ]
实证2 Z- g A2 w( ^; ^. W
用sizeof(),float *,int*,double *....都是4字节没有存储类型信息只有地址信息
2 Y/ Y6 V$ C: L' W) z反汇编一段代码来看看
- m$ D4 c& E. G6 V实例1:7 r4 j3 u0 T4 `/ w! @2 A
#include <stdio.h> int gi; int * pi; void main(int argc, char **argv) { pi = &gi; *pi = 12; } 1: #include <stdio.h> 2: int gi; 3: int * pi; 4: void main(int argc, char **argv) 5: { 00411370 push ebp 00411371 mov ebp,esp 00411373 sub esp,0C0h 00411379 push ebx 0041137A push esi 0041137B push edi 0041137C lea edi,[ebp+FFFFFF40h] 00411382 mov ecx,30h 00411387 mov eax,0CCCCCCCCh 0041138C rep stos dword ptr es:[edi] 6: pi = &gi; 0041138E mov dword ptr ds:[00417140h],417144h 7: *pi = 12; 00411398 mov eax,dword ptr ds:[00417140h] 0041139D mov dword ptr [eax],0Ch 8: } 004113A3 xor eax,eax 004113A5 pop edi 004113A6 pop esi 004113A7 pop ebx 004113A8 mov esp,ebp 004113AA pop ebp 004113AB ret
0 O: R% Z, p/ ]- @( e3 E
) X( ?2 o) Q- {4 s/ F/ e实例2:+ X' u: R9 n: j3 r: m; X; n! `5 I& t
#include <stdio.h> short gs; short * ps; void main(int argc, char **argv) { ps = &gs; *ps = 12; }
, U: b# j/ h M( I2 U 1: #include <stdio.h> 2: short gs; 3: short * ps; 4: void main(int argc, char **argv) 5: { 00411370 push ebp 00411371 mov ebp,esp 00411373 sub esp,0C0h 00411379 push ebx 0041137A push esi 0041137B push edi 0041137C lea edi,[ebp+FFFFFF40h] 00411382 mov ecx,30h 00411387 mov eax,0CCCCCCCCh 0041138C rep stos dword ptr es:[edi] 6: ps = &gs; 0041138E mov dword ptr ds:[00417140h],417144h 7: *ps = 12; 00411398 mov eax,0Ch 0041139D mov ecx,dword ptr ds:[00417140h] 004113A3 mov word ptr [ecx],ax 8: } 004113A6 xor eax,eax 004113A8 pop edi 004113A9 pop esi 004113AA pop ebx 004113AB mov esp,ebp 004113AD pop ebp 004113AE ret F' X {$ ?# b
7 ]8 C, v& F, k0 M6 j! S, b
实例3:
9 q% C+ J* [8 Y [' Y$ {#include <stdio.h> char gc; char * pc; void main(int argc, char **argv) { pc = &gc; *pc = 12; }
( f- N( j; B- |0 K3 A/ |) e( q) L 1: #include <stdio.h> 2: char gc; 3: char * pc; 4: void main(int argc, char **argv) 5: { 00411370 push ebp 00411371 mov ebp,esp 00411373 sub esp,0C0h 00411379 push ebx 0041137A push esi 0041137B push edi 0041137C lea edi,[ebp+FFFFFF40h] 00411382 mov ecx,30h 00411387 mov eax,0CCCCCCCCh 0041138C rep stos dword ptr es:[edi] 6: pc = &gc; 0041138E mov dword ptr ds:[00417140h],417144h 7: *pc = 12; 00411398 mov eax,dword ptr ds:[00417140h] 0041139D mov byte ptr [eax],0Ch 8: } 004113A0 xor eax,eax 004113A2 pop edi 004113A3 pop esi 004113A4 pop ebx 004113A5 mov esp,ebp 004113A7 pop ebp 004113A8 ret
8 Z5 ]. }" l8 K' T2 ^( R; F% O& M! m* Z, w6 \7 A$ R) D- o
可以看出指针的类型信息决定了赋值/读取时读/写多少个字节。
9 r; p: ^7 k$ g2 u' g! X; ~读/写多少字节的信息不是存放在指针变量中,而是放到了与该地址相关的赋值指令中。
0 W+ z& z9 I) C% x4 e |