2. In information engineering, pointer is a computer language variable used to indicate the memory address or register in the central processing unit (CPU). Pointers generally appear in languages close to machine language, such as assembly language or C language. Object-oriented languages such as Java generally avoid using pointers. Pointers usually point to functions or variables. When using a pointer, the program can directly use the memory address stored in this pointer, or use the value of a variable or function tree stored in this address.
Everyone thinks that the reason why C language is powerful and free lies in the flexible use of pointers to a great extent. So it is no exaggeration to say that pointer is the soul of C language. At the same time, this statement has also caused many people's misunderstanding. It seems that only pointers in C language can be regarded as pointers. Basic does not support pointers, so it doesn't matter here. In fact, pascal itself supports pointers. From the original pascal to the present, object pascal can be said to be no worse than the pointer in C language in the use of pointers.
The following contents are divided into eight parts, namely
I. definition of type pointer
Second, the definition of non-type pointers.
Thirdly, the dereference of pointers.
Fourth, take the address (pointer assignment)
V. Pointer operation
Dynamic storage and allocation of intransitive verbs
Seven, the operation of character array
Eight, function pointer
First, the definition of type pointer. For a pointer to a specific type, it is defined in C as follows:
int * ptr
char * ptr
How is the equivalent object pascal defined?
defined variable
ptr:^integer;
ptr:^char;
Actually, it's just the difference between symbols.
Second, the definition of untyped pointers. C contains void * type, which is a pointer that can point to any type of data. The object pascal is for it.
A special type is defined: pointer. So,
Ptr: pointer;
Just like in C.
void * ptr
Equivalent to.
Third, the dereference of pointers. To dereference a pointer (that is, take out the value of the area pointed by the pointer), the syntax of C is (*ptr), object.
Pascal is ptr^.
Fourth, take the address (pointer assignment). Get the address of an object and assign it to a pointer variable. The syntax of C language is
Ptr = & object;
The object pascal is
ptr:= @ object;
It's just different symbols.
Five, pointer operation. In C, you can move the pointer, for example:
char a[20];
char * ptr = a;
ptr++;
ptr+= 2;
When executing ptr++; , the compiler will generate code to make ptr advance by sizeof(char) step, after which ptr will point to a.
[ 1]。 ptr+= 2; This sentence pushes ptr forward by two sizeof(char) steps. Similarly, let's see how to implement it in object pascal.
Now:
defined variable
Answer: char of array [1... 20]
ptr:pchar; //pchar can be regarded as char.
begin
ptr:= @ a;
Inc(ptr); //This sentence is equivalent to Pt R++of C;
inc(ptr,2); //This sentence is equivalent to ptr+= 2; of C;
End;
However, in pascal, such an operation is only allowed for typed pointers, but not for untyped pointers.
Sixth, dynamic memory allocation. In C, malloc () library function is used to allocate memory, and free () function is used to release memory. Code like this:
int *ptr,* ptr 2;
int I;
ptr =(int *)malloc(sizeof(int)* 20);
ptr2 = ptr
for(I = 0; I & lt20; i++){
* ptr = I;
ptr++;
}
Free (ptr2);
In object pascal, the function of dynamically allocating memory is getmem (), and the corresponding release function is freemem () (traditional
There are new () and dispose () functions to get memory in pascal, but new () can only get the memory size of a single entity of an object, and cannot get a connection.
A memory block that continues to store multiple objects). So, the code of object pascal, which is equivalent to the code of C above, is:
var ptr,ptr 2:^integer;
I: integer;
begin
getmem(ptr,sizeof(integer)* 20);
//This sentence is equivalent to ptr = (int *) malloc (sizeof (int) * 20) of C;
ptr 2:= ptr; //Keep the original pointer position
For i := 0 to 19 do.
begin
Ptr: = me;
Inc(ptr);
End;
freemem(ptr 2);
End;
For the above example (whether C version or object pascal version), we should pay attention to one problem, that is, allocation.
The unit of memory is bytes, so when using getmem, if the second parameter is written as 20, an error will occur.
(Memory access out of bounds). Because getmem(ptr, 20); In fact, only 20 bytes of memory space is allocated, and the size of a plastic surgery is four.
Bytes, it is illegal to access all elements after the fifth (as is the malloc () parameter).
Seven, the operation of the character array. C language, there is no string type, therefore, strings are all realized by character arrays, so it is also
There is a set of library functions starting with str for the operation of character arrays, such as the following code:
char str[ 15];
char * pstr
strcpy(str,“teststr”);
strcat(str," _ testok ");
pstr =(char *)malloc(sizeof(char)* 15);
strcpy(pstr,str);
printf(pstr);
Free of charge (PSTR);
In object pascal, you can easily perform various operations on strings by using string types. But sometimes,
Our pascal code needs to interact with the code of C (for example, calling the dll written by C with the code of object pascal or using object pascal).
If the written dll is ready to allow writing client code in C), you cannot use the string type, but you must use the number of characters common to both languages.
Group. In fact, object pascal provides a complete set of character array operation functions, which is completely similar to the object pascal in the code above C.
The version is like this:
Variable string: char of array [1.. 15]
pstr:pchar; //pchar is also called char.
begin
strcopy(@str,' teststr '); //In C, the array name can be directly used as a pointer to the first address of the array.
//But pascal is not like this. You should add the address operator before str.
strcat(@str,' _ testok ');
getmem(pstr,sizeof(char)* 15);
strcopy(pstr,@ str);
Write (pstr);
freemem(pstr);
End;
Eight, function pointer. Use function pointers when dynamically calling functions in dll. Suppose a piece of code written in C is as follows:
typedef int(* pvfn)(int); //Define the function pointer type
int main()
{
hm odule hm odule = loadlibrary(" test . dll ");
pvfn pvfn = null
pvfn =(pvfn)getprocaddress(hmodule," function 1 ");
pvfn(2);
free library(hmodule);
}
Personally, I feel that the syntax of typedef code defining function pointer type in C language is somewhat obscure, while the same code is in
The object pascal is very easy to understand:
type pvfn = function(para:integer):integer;
defined variable
fn:pvfn;
//It can also be directly defined here, for example: fn: function (para: integer): integer;
hm:hm odule;
begin
hm:= loadlibrary(' test . dll ');
fn := getprocaddress(hm,' function 1 ');
fn(2);
Free library (hm);
End;