指针类型在内存里是以什么形式存的?(50分)

  • 主题发起人 主题发起人 Cobra
  • 开始时间 开始时间
C

Cobra

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用变体存指针,可不知道指针用整型够不够存。
下面这段代码可以正常运行不知道有没有什么隐患。
var
v:Variant;
begin
v := integer(Label1);
TLabel(integer(v)).Caption := 'aa';
end;
 
好像是32位整数
你的程序应该没问题,呵呵


如果不放心,可以用int64
 
为什么要用Variants类型?,你可以用指向指针的动态数组来做.
另一方向,根据变体类型在System单元中的定义,也可以看出,变体类型是不支持指针的.
TVarData = packed record
VType: TVarType;
case Integer of
0: (Reserved1: Word;
case Integer of
0: (Reserved2, Reserved3: Word;
case Integer of
varSmallInt: (VSmallInt: SmallInt);
varInteger: (VInteger: Integer);
varSingle: (VSingle: Single);
varDouble: (VDouble: Double);
varCurrency: (VCurrency: Currency);
varDate: (VDate: TDateTime);
varOleStr: (VOleStr: PWideChar);
varDispatch: (VDispatch: Pointer);
varError: (VError: LongWord);
varBoolean: (VBoolean: WordBool);
varUnknown: (VUnknown: Pointer);
varShortInt: (VShortInt: ShortInt);
varByte: (VByte: Byte);
varWord: (VWord: Word);
varLongWord: (VLongWord: LongWord);
varInt64: (VInt64: Int64);
varString: (VString: Pointer);
varAny: (VAny: Pointer);
varArray: (VArray: PVarArray);
varByRef: (VPointer: Pointer);
);
1: (VLongs: array[0..2] of LongInt);
);
2: (VWords: array [0..6] of Word);
3: (VBytes: array [0..13] of Byte);



{ Variant type codes (wtypes.h) }

varEmpty = $0000
{ vt_empty }
varNull = $0001
{ vt_null }
varSmallint = $0002
{ vt_i2 }
varInteger = $0003
{ vt_i4 }
varSingle = $0004
{ vt_r4 }
varDouble = $0005
{ vt_r8 }
varCurrency = $0006
{ vt_cy }
varDate = $0007
{ vt_date }
varOleStr = $0008
{ vt_bstr }
varDispatch = $0009
{ vt_dispatch }
varError = $000A
{ vt_error }
varBoolean = $000B
{ vt_bool }
varVariant = $000C
{ vt_variant }
varUnknown = $000D
{ vt_unknown }
//varDecimal = $000E
{ vt_decimal } {UNSUPPORTED}
{ undefined $0f } {UNSUPPORTED}
varShortInt = $0010
{ vt_i1 }
varByte = $0011
{ vt_ui1 }
varWord = $0012
{ vt_ui2 }
varLongWord = $0013
{ vt_ui4 }
varInt64 = $0014
{ vt_i8 }
//varWord64 = $0015
{ vt_ui8 } {UNSUPPORTED}

{ if adding new items, update Variants' varLast, BaseTypeMap and OpTypeMap }
varStrArg = $0048
{ vt_clsid }
varString = $0100
{ Pascal string
not OLE compatible }
varAny = $0101
{ Corba any }
varTypeMask = $0FFF;
varArray = $2000;
varByRef = $4000;

资料来源: Delphi5开发人员指南 第33页.
原句如下:
.....
注意 在上面的列表代码中你可能发现,Variant不能代表指针和类.




TVarData = packed record
VType: TVarType;
case Integer of
0: (Reserved1: Word;
case Integer of
0: (Reserved2, Reserved3: Word;
case Integer of
varSmallInt: (VSmallInt: SmallInt);
varInteger: (VInteger: Integer);
varSingle: (VSingle: Single);
varDouble: (VDouble: Double);
varCurrency: (VCurrency: Currency);
varDate: (VDate: TDateTime);
varOleStr: (VOleStr: PWideChar);
varDispatch: (VDispatch: Pointer);
varError: (VError: LongWord);
varBoolean: (VBoolean: WordBool);
varUnknown: (VUnknown: Pointer);
varShortInt: (VShortInt: ShortInt);
varByte: (VByte: Byte);
varWord: (VWord: Word);
varLongWord: (VLongWord: LongWord);
varInt64: (VInt64: Int64);
varString: (VString: Pointer);
varAny: (VAny: Pointer);
varArray: (VArray: PVarArray);
varByRef: (VPointer: Pointer);
);
1: (VLongs: array[0..2] of LongInt);
);
2: (VWords: array [0..6] of Word);
3: (VBytes: array [0..13] of Byte);



{ Variant type codes (wtypes.h) }

varEmpty = $0000
{ vt_empty }
varNull = $0001
{ vt_null }
varSmallint = $0002
{ vt_i2 }
varInteger = $0003
{ vt_i4 }
varSingle = $0004
{ vt_r4 }
varDouble = $0005
{ vt_r8 }
varCurrency = $0006
{ vt_cy }
varDate = $0007
{ vt_date }
varOleStr = $0008
{ vt_bstr }
varDispatch = $0009
{ vt_dispatch }
varError = $000A
{ vt_error }
varBoolean = $000B
{ vt_bool }
varVariant = $000C
{ vt_variant }
varUnknown = $000D
{ vt_unknown }
//varDecimal = $000E
{ vt_decimal } {UNSUPPORTED}
{ undefined $0f } {UNSUPPORTED}
varShortInt = $0010
{ vt_i1 }
varByte = $0011
{ vt_ui1 }
varWord = $0012
{ vt_ui2 }
varLongWord = $0013
{ vt_ui4 }
varInt64 = $0014
{ vt_i8 }
//varWord64 = $0015
{ vt_ui8 } {UNSUPPORTED}

{ if adding new items, update Variants' varLast, BaseTypeMap and OpTypeMap }
varStrArg = $0048
{ vt_clsid }
varString = $0100
{ Pascal string
not OLE compatible }
varAny = $0101
{ Corba any }
varTypeMask = $0FFF;
varArray = $2000;
varByRef = $4000;


 
如楼上所说,Delphi对Variant类型有特殊的处理,如果不是特殊情况,建议还是用基本类型来实现。
如果要存放指针类型,任何长度为4Bytes的类型都可以胜任(Integer,DWord,Pointer,Single...)
您可以看一看Delphi的帮助中有关变量类型的部分。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
694
import
I
I
回复
0
查看
724
import
I
后退
顶部