马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kangels 于 2013-3-19 10:23 编辑 7 T) M. `. g( ~
& ? w, f# [; p- S3 @+ J0 \ q
丢失的类型信息* ^: e3 j5 q1 e; {% `% H: j2 |
猜测0 m# k S4 F4 C. \/ Y
指针变量应该存储两方面的信息:地址,类型信息
6 `( _! @1 C& L; E9 `8 `0 O
& E6 S) X; @- ~ T" U2 G实证0 X4 ~- l% O" b. ^$ Z- k; i" Y
用sizeof(),float *,int*,double *....都是4字节没有存储类型信息只有地址信息6 I" G. O5 s" Q/ N! W- T
反汇编一段代码来看看0 v3 J5 \0 ]+ w
实例1:
, y r$ m. S4 y0 l' ?" q) x#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 : d" {: |# B# v# I& w4 o
) i5 l# P% c$ B# N' n
实例2:$ T; D% s9 E4 Z+ e
#include <stdio.h> short gs; short * ps; void main(int argc, char **argv) { ps = &gs; *ps = 12; } $ E# M; h: n" V# |; \
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 ( q) [1 ]! R$ d0 Y8 t* ~4 y
( Y, D+ o( e2 [8 J I- j实例3:
0 U2 J1 s# F4 Z/ P# u& O#include <stdio.h> char gc; char * pc; void main(int argc, char **argv) { pc = &gc; *pc = 12; }
. \: F* A: Y/ n+ w8 K6 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 ' L( @3 I# w8 @) i' L, b
" q0 \: F) ~# A6 F4 B2 u4 I可以看出指针的类型信息决定了赋值/读取时读/写多少个字节。% a |+ i/ b$ D8 R1 q, h4 i' H
读/写多少字节的信息不是存放在指针变量中,而是放到了与该地址相关的赋值指令中。- V8 ]# R4 }+ h) z' B9 J
|