Go Pascal and C's pointers

Source: Internet
Author: User

It is thought that the reason why C is strong, and its freedom, is largely reflected in its flexible use of pointers. Therefore, it is not too important to say that the pointer is the soul of C language. At the same time, this argument also makes a lot of people misunderstanding, it seems that only the C language pointer can be counted. Basic does not support pointers, regardless. In fact, the Pascal language itself also supports pointers. The Object Pascal, from the very beginning of Pascal's development, can be said to be in the hands of the pointer, not inferior to the C language pointer.

The following are divided into eight sections, namely:
Definition of a type pointer
Ii. definition of an untyped pointer
Iii. dereferencing A pointer
Iv. Fetch Address (pointer assignment value)
Five, pointer arithmetic
VI. Dynamic Memory allocation
Vii. Arithmetic of character array
Eight, function pointers


The definition of a type pointer. For pointers to specific types, this is defined in C:
int *ptr;
Char *ptr;
How is the equivalent of the object Pascal defined?
Var
PTR: ^integer;
PTR: ^char;
In fact, the difference between the symbols.

Second, the definition of the untyped pointer. There is a void * type in C, which is a pointer to any type of data. Object Pascal defines a specific type for it: Pointer. So
Ptr:pointer;
As in C.
void *ptr;
Equivalent.

Third, dereference the pointer. To remove a pointer reference (that is, the value of the area to which the pointer refers), the syntax for C is (*ptr), and Object Pascal is ptr^.

Four, take the address (pointer assignment value). Takes an address of an object and assigns it to a pointer variable, the syntax of C is
PTR = &Object;
The Object Pascal is
PTR: = @Object;
It's just a sign of the difference.

Five, the pointer operation. In C, you can move a pointer to an operation, such as:
Char a[20];
Char *ptr=a;
ptr++;
ptr+=2;
When the ptr++ is executed, the compiler generates code that lets PTR advance the sizeof (char) step, and then ptr points to a[1]. ptr+=2, which makes the PTR advance two sizeof (char) size step. Again, let's take a look at how Object Pascal is implemented:
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++ of C;
Inc (PTR, 2); This sentence is equivalent to the ptr+=2 of C;
End

Six, dynamic memory allocation. C, using the malloc () library function to allocate memory, the free () function frees memory. Code like this:
int *ptr, *PTR2;
int i;
ptr = (int*) malloc (sizeof (int) * 20);
PTR2 = ptr;
for (i=0; i<20; i++) {
*ptr = i;
ptr++;
}
Free (PTR2);
In Object Pascal, the function that allocates memory dynamically is GETMEM (), the corresponding deallocation function is Freemem () (the function that acquires memory in traditional Pascal is new () and Dispose (), but new () can only get the memory size of a single entity of an object. Cannot get contiguous blocks of memory that hold multiple objects). Therefore, the code of Object Pascal, which is equivalent to the code of the preceding section C, is:
var ptr, ptr2: ^integer;
I:integer;
Begin
Getmem (PTR, sizeof (integer) * 20);
This sentence is equivalent to C's ptr = (int*) malloc (sizeof (int) * 20);
PTR2: = ptr; Keep the original pointer position
For I: = 0 To
Begin
ptr^: = i;
INC (PTR);
End
Freemem (PTR2);
End
For this example (either the C version or the Object Pascal version), it is important to note that the allocation of memory is in bytes (byte), so when using Getmem, its second parameter if it is assumed to be 20, then there will be a problem (memory access out of bounds). Since Getmem (PTR, 20) is actually allocated only 20 bytes of memory, and the size of an integer is four bytes, then all elements after the fifth access are illegal (for malloc () parameters as well).

Seven, the operation of the character array. In the C language, there is no string type, so the string is implemented with a character array, so there is a set of STR-preceded library functions for the operation of the character array, 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 (PSTR);
In Object Pascal, there is a string type, which makes it easy to perform various operations on the string. However, sometimes our Pascal code needs to interact with C's code (e.g., a DLL written in C with Object Pascal, or a DLL written in Object Pascal to allow the code to write the client in C), you cannot use the string type. Instead, you must use a character array that is common to both languages. In fact, Object Pascal provides an arithmetic function that exactly resembles the whole set of character arrays of C, which is the object Pascal version of the above code:
var Str:array [1..15] of char;
Pstr:pchar; Pchar is ^char.
Begin
Strcopy (@str, ' teststr '); In C, the name of the array can be used directly as a pointer to the array's first address
But Pascal is not like this, so add an operator to the address before Str
StrCat (@str, ' _testok ');
Getmem (pstr, sizeof (char) * 15);
Strcopy (Pstr, @str);
Write (PSTR);
Freemem (PSTR);
End

Eight, function pointers. A function pointer is used when a function in a DLL is called dynamically. Let's say that a section of code written in C is as follows:
typedef int (*PVFN) (int); Defining function pointer types
int main ()
{
hmodule hmodule = LoadLibrary ("Test.dll");
PVFN PVFN = NULL;
PVFN = (PVFN) GetProcAddress (hmodule, "Function1");
PVFN (2);
FreeLibrary (hmodule);
}
In my personal sense, the syntax of a typedef code that defines a function pointer type in C is somewhat obscure, and the same code is quite understandable in Object Pascal:
Type PVFN = Function (Para:integer): Integer;
Var
FN:PVFN;
can also be defined directly here, such as: Fn:function (Para:integer): Integer;
Hm:hmodule;
Begin
HM: = LoadLibrary (' Test.dll ');
fn: = GetProcAddress (hm, ' Function1 ');
FN (2);
FreeLibrary (HM);
End

Report:
The pointer function in Delphi is very powerful, all the pointers in C can be implemented in Delphi. It is only a misunderstanding that the Delphi pointer is not a strong point (or the mechanism of the pointer smattering). Due to the limitations of the Pascal language, the use of Delphi pointers in many cases requires coercion of type conversions. There are many pointer types available in Delphi, and it is very handy that you can customize your own pointer types.


An experience: To master a data type and be able to apply it flexibly, it is a good idea not to consider what type is the name, but only to consider how many bytes this type of variable will occupy. Types with the same number of bytes can be considered to be of the same type:-), providing different types just to make it easier for the compiler to find errors. For example: Integer, Pointer, PChar, tsmallpoint even array [0..4] of char you can use them as the same type (with this idea, you can achieve a lot of program flexibility and code efficiency). So what I don't understand is that pointers are not supported in Java (so I also think it is absolutely not possible to write very efficient programs in Java, and there are a lot of things that can be done in C/c++/delphi in Java, which requires a complex process that consumes a lot of extra memory to achieve the same purpose). As a matter of question, the solution in Delphi and C is no different depending on your problem.
Var
A:pointer; or any other pointer type
Begin
Getmem (A, 10); Allocate 10 bytes
Pinteger (a) ^: = Integer (FORM1); Will Form1 (in fact all the classes in Delphi can be thought of as pointers)
Save to A[0..3]
Here is the difference from C, which requires a pointer type conversion in Delphi,
The main purpose is to "cheat" the compiler, lest the program cannot be compiled through.
(PChar (a) +sizeof (Integer)) ^: = ' B '; Save a character ' B ' in a[4]
....
End

Cases:

Var
P:^char;
I:integer;
s:string;
Begin
P:[email protected];
For I:=0 to
Begin
S:=s+^p; Error
Inc (P);
End
ShowMessage (s);
End

Go Pascal and C's pointers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.