마음의 안정을 찾기 위하여 - [Delphi] Pointer Arithmetic in Delphi
2260914
588
315
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
[Delphi] Pointer Arithmetic in Delphi
Delphi/Etc Tip | 2019/07/25 15:45

Delphi is based on Object Pascal, which is a great programming language. Delphi is also a RAD (Rapid Application Development) tool for Windows, Android, iOS. It can be compiled for both 32-bit and 64-bit native code. In Delphi, you have powerful stuffs such as pointers as other programming languages such as C/C++. With pointers, you can increment or decrement and do some arithmetic. For example,

var
    p: PInteger;
    q: array [1..3] of integer;
begin
    p := @q[1];
    p^ := 1;
    Inc(p);
    p^ := 2;
    Inc(p);
    p^ := 3;
end;

However, if you want to use pointers in an expression, such as,

Pointer(Integer(data) + 2); // data is a pointer

you can cast it first to numerical numbers (treat pointers as integers). Some people prefer casting to unsigned integers (LongWord) instead of Integer because it doesn’t make sense for negative memory address.

Noted, that on 64-bit OS, the pointer is actually 8 size instead of 4 size, so therefore, the above castings will not work on 64-bit OS, which should be replaced by nativeint or nativeuint which is size 4 under 32-bit compiler and size 8 under 64-bit compiler.

Before Delphi 2009, when {$POINTERMATH ON} is not present, therefore, you could cast the pointers to PAnsiChar which allows pointer arithmetic (without casting to integers), on and after D2009, you could cast the pointers to PByte and turn the compiler directive on {$POINTERMATH ON} which is local scope (effects in current unit) for other types.

As a general rule of thumb, pointers are not numbers and you should avoid converting them to be integers. It is almost always best to let pointers be pointers. You can always perform pointer arithmetic on types PAnsiCharPWideChar and PByte, and for other pointer types you can use {$POINTERMATH ON} to enable pointer arithmetic.

The following will be recommended on and after D2009,

{$POINTERMATH ON}
Pointer(PByte(data) + 2); // data is a pointer

And use the following before D2009, e.g. D2007

Pointer(PAnsiChar(data) + 2); // data is a pointer

 

2019/07/25 15:45 2019/07/25 15:45
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [74][75][76][77][78][79][80][81][82] ... [936] :
«   2024/03   »
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
전체 (936)
출판 준비 (0)
My-Pro... (41)
사는 ... (517)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (5)
Database (12)
리눅스 (29)
Windows (25)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (21)
잡다한... (6)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)