Talking about Object Pascal's pointer

zhaozj2021-02-08  264

Talking about Object Pascal's pointer

Nicrosoft (nicrosoft@sunistudio.com) 2001.8.26 (http://www.sunistudio.com/nicrosoft/ East Day Document: http://www.sunistudio.com/asp/sunidoc.asp)

Everyone believes that the C language is powerful, and its freedom is reflected in its flexible pointer. Therefore, it is said that the pointer is the soul of the C language. At the same time, this statement also makes many people misunderstand that only the C language pointer can be considered. BASIC does not support pointers, here in this regard. In fact, the Pascal language itself is also supporting pointers. Object Pascal from the initial Pascal to this day, can be said to be in the pointer, not inferior to the C language pointer.

The following is divided into eight parts, which are one, definition of the type pointer. Second, the definition of the type of pointer. Three, the release reference 4, take the address (pointer assignment) 5. Pointer operation 6, dynamic memory allocation seven, character array Operation eight, function pointer

First, the definition of type pointer. For a specific type of pointer, it is defined in C: INT * PTR; char * PTR; how is the equivalent Object Pascal defined? Var PTR: ^ Integer; PTR: ^ char; actually is the difference in symbols.

Second, there is no definition of type pointer. There is a void * type in c, which is to point to pointers of any type of data. Object Pascal defines a special type: Pointer. So, PTR: POINTER; is equivalent to the Void * PTR;

Third, the release of the pointer. To release the pointer reference (ie, the value of the area refers to the area refers to the pointer), the syntax of C is (* PTR), Object Pascal is Ptr ^.

Fourth, take the address (the pointer assignment). Take the address of the object and assign it to the pointer variable, the syntax of C is ptr = & object; Object Pascal is PTR: = @Object; it is only the difference between symbols.

Five, pointer operation. In c, the pointer can be moved, such as: char A [20]; char * ptr = a; PTR ; PTR = 2; When performing PTR ;, the compiler will generate stepof (char) step Long code, then PTR will point to A [1]. PTR = 2; This sentence makes the PTR forward into two SizeOf (char) steps. Similarly, let's take a look at how to implement in Object Pascal: Var A: Array [1..20] of char; ptr: pchar; // pchar can be seen as ^ Char Begin PTR: = @a; inc (PTR); / / This sentence is equivalent to the PTR ; Inc (PTR, 2); // This sentence is equivalent to the PTR = 2; END;

Sixth, dynamic memory allocation. C, use the malloc () library function to allocate memory, free () function releases memory. Such as this code: INT * PTR, * PTR2; INT I; PTR = (int *) malloc (intend * 20); PTR2 = PTR; for (i = 0; i <20; i ) {* PTR = I; PTR ;} free (PTR2); Object Pascal, dynamically allocated memory function is getMem (), and the corresponding release function is freeMem () (more functions in traditional pascal is new () and Dispose ( ), But new () can only obtain the memory size of the individual entities of the object, and cannot obtain a continuous memory block of multiple objects). Therefore, the code equivalent of the code equivalent with the above paragraph C is: VAR PTR, PTR2: ^ Integer; i: integer; begin getmem (PTR, SIZEOF (Integer) * 20); // This sentence is equivalent C PTR = (int *) malloc (intend * 20); Ptr2: = Ptr; // Reserved original pointer position for i: = 0 to 19 do begin Ptr ^: = i; inc (PTR); END FreeMem (PTR2); END; for the above example (whether C versions, or Object Pascal version), pay attention to a problem, that is, the unit allocated memory is byte, so when using getmem, Its second parameter If you want to write 20, then you will have problems (memory access offline). Because GetMem (PTR, 20); actually only assigned 20 bytes of memory space, and a shaping size is four bytes, then all elements after accessing the fifth are illegal (for malloc () The parameters are the same).

7. The operation of the character array. In the C language, there is no string type, so the string is implemented with a character number, so there is also a set of STR header library functions to perform the array of characters, as follows: char STR [15]; char STR [15]; char STR [15]; char STR [15]; char * pstr; strcpy (str, "teststr"); strcat (str, "_testok"); pstr = (char *) malloc (sizeof (char) * 15); struffpy (pstr, str); Printf (PSTR); Free (PSTR); In Object Pascal, there is a string type, so it can make a variety of computments for strings. However, sometimes our Pascal code needs to interact with C (such as: Call C written with the CSCAL code, or written with the CSCAL, you can use the Cerect Pascal code), you cannot use the String type. And you must use two languages ​​universal characters. In fact, Object Pascal provides an operation function of a full-character number of characters, the Object Pascal version of the above code is like this: Var str: array [1..15] of char; pstr: pchar; // pchar That is, ^ charism strcopy; // In C, the name of the array can be used directly as a group head address pointer to // but Pascal is not like this, so the STR should be added to add the address. Operator strcat (@str, '_testok'); getMem (PSTR, SIZEOF (CHAR) * 15); strcopy (pstr, @str); Write (PSTR); FreeMem (PSTR);

转载请注明原文地址:https://www.9cbs.com/read-2057.html

New Post(0)