马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kangels 于 2013-3-19 10:23 编辑 ; ^1 ~( s" G8 y ^3 T+ w% D
: ~4 R$ O" ?! v: t3 h1 F; k丢失的类型信息
9 W8 `: N7 v& Q+ |% i猜测
/ \5 i/ b7 p2 v2 `0 \指针变量应该存储两方面的信息:地址,类型信息0 \! j+ d6 m2 ~! H3 a
! y: A d5 W7 |, s& q( v/ W! t
实证$ u2 x F' G+ L
用sizeof(),float *,int*,double *....都是4字节没有存储类型信息只有地址信息
2 a7 b% H1 v# D0 e1 ?: d; S反汇编一段代码来看看
# ?7 H( B; @% l实例1:
: P$ C( ?/ a/ D7 q1 ?+ B; X* c6 T#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
7 o+ q$ r! ~' ?; T* O0 E& Q) p% [8 L5 y- C5 i r% s( g6 e Q$ X7 f
实例2:
$ K1 ~" K8 J; p#include <stdio.h> short gs; short * ps; void main(int argc, char **argv) { ps = &gs; *ps = 12; } ! y) ~ l: p1 B
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 + t9 }: y* f& o+ U9 o$ [/ A
2 {7 n- O% ^( i2 B) C
实例3:
5 @" u( [9 }0 k#include <stdio.h> char gc; char * pc; void main(int argc, char **argv) { pc = &gc; *pc = 12; } 6 O0 F) ^, o p
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 2 H ?9 k/ v0 F, B0 }3 x E9 i3 c3 b
( q9 L' K, L k/ R
可以看出指针的类型信息决定了赋值/读取时读/写多少个字节。6 B% k0 r; T# _: U7 R
读/写多少字节的信息不是存放在指针变量中,而是放到了与该地址相关的赋值指令中。9 F5 e3 e' M# U4 T, s
|