About Pascal pointer)

Source: Internet
Author: User

We all think that the reason why C language is powerful and its freedom is largely reflected in its flexible pointer usage. Therefore, pointer is the soul of the C language, and it cannot be said at all. At the same time, this argument has caused many people to misunderstand. It seems that only pointers in the C language can calculate pointers. Basic does not support pointers. In fact, Pascal supports pointers. From the initial development of Pascal to the present, Object Pascal can be said that in Pointer application, it will not be inferior to the pointer in C language.

The following content is divided into eight parts:
I. Definition of Type pointers
Ii. Definition of No-type pointer
3. Release pointer references
4. Get the address (assign a pointer)
5. pointer operations
6. Dynamic Memory Allocation
VII. Operations on character Arrays
8. function pointer

1. Type pointer definition. For pointers pointing to specific types, the definition in C is as follows:
Int * PTR;
Char * PTR;
How is the equivalent Object Pascal defined?
VaR
PTR: ^ integer;
PTR: ^ char;
It is actually the difference between symbols.

2. No type pointer definition. C has the void * type, that is, the pointer that can point to any type of data. Object Pascal defines a special type for it: pointer. So,
PTR: pointer;
In C
Void * PTR;
Equivalent.

3. unreference the pointer. To remove the pointer reference (that is, to retrieve the value of the region pointed by the pointer), the C syntax is (* PTR), and the Object Pascal is PTR ^.

4. Get the address (assign a pointer ). Take the address of an object and assign it to the pointer variable. The C syntax is
PTR = & object;
Object Pascal is
PTR: = @ object;
It is only a symbol difference.

5. pointer operation. In C, the pointer can be moved, for example,
char a [20];
char * PTR = A;
PTR ++;
PTR + = 2;
when PTR ++ is executed, the compiler generates a sizeof (char) Code of the step, PTR points to a [1]. PTR + = 2; this statement makes the PTR move forward to two sizeof (char) step sizes. Similarly, let's take a look at how to implement Object Pascal:
var
A: array [1 .. 20] of char;
PTR: pchar; // pchar can be viewed as ^ char
begin
PTR :=@ A;
Inc (PTR ); // This sentence is equivalent to C's PTR ++;
Inc (PTR, 2); // This sentence is equivalent to C's PTR ++ = 2;
end;

6. dynamic memory allocation. In C, use the malloc () library function to allocate memory, and the free () function to release the 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 ++;
}< br> free (ptr2);
in Object Pascal, the function for dynamically allocating memory is getmem (), and the corresponding release function is freemem () (in traditional Pascal, the functions used to obtain memory are new () and dispose (), but new () can only obtain the memory size of a single object of an object, cannot obtain memory blocks that store multiple objects consecutively ). Therefore, the code of the Object Pascal equivalent to the above section C is:
var PTR, ptr2: ^ integer;
I: integer;
begin
getmem (PTR, sizeof (integer) * 20);
// This sentence is equivalent to PTR = (int *) of C *) malloc (sizeof (INT) * 20);
ptr2: = PTR; // retain the original pointer position
for I: = 0 to 19 do
begin
PTR ^: = I;
Inc (PTR);
end;
freemem (ptr2 );
end;
you must pay attention to one of the preceding examples (in C or Pascal, the unit of memory allocation is byte. Therefore, when getmem is used, if the second parameter is set to 20 There will be a problem (memory access out of bounds ). Because getmem (PTR, 20); actually only 20 bytes of memory is allocated, and the size of an integer is four bytes, all elements after the fifth access are invalid (the same for the malloc () parameter ).

7. character array operation. In C, there is no string type. Therefore, strings are all implemented using character arrays. Therefore, there is also a set of database functions with str headers for character array operations, as shown in 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, the string type is available, therefore, you can easily perform various operations on strings. However, sometimes our Pascal code needs to interact with C code (for example: if you use the Object Pascal code to call the DLL written in C or use the DLL written in Object Pascal to allow C to write the client code), you cannot use the string type, you must use a string array that is common to both languages. In fact, Object Pascal provides a complete set of character array operation functions similar to C. The Object Pascal version of the code above is as follows:
var STR: array [1 .. 15] of char;
pstr: pchar; // pchar that is ^ char
begin
strcopy (@ STR, 'teststr '); // In C, the array name can be directly used as the first address pointer of the array
// but Pascal is not like this, therefore, the address-taking operator
strcat (@ STR, '_ testok') must be added before STR;
getmem (pstr, sizeof (char) * 15 );
strcopy (pstr, @ Str);
write (pstr);
freemem (pstr);
end;

8. function pointer. When you call a function in a DLL dynamically, the function pointer is used. Assume that a piece of code written in C is as follows:
Typedef int (* pvfn) (INT); // defines the function pointer type.
Int main ()
{
Hmodule = loadlibrary ("test. dll ");
Pvfn = NULL;
Pvfn = (pvfn) getprocaddress (hmodule, "function1 ");
Pvfn (2 );
Freelibrary (hmodule );
}
In my personal sense, the syntax of typedef Code defining function pointer type in C language is a bit obscure, and the same code is very easy to understand in Object Pascal:
Type pvfn = function (para: integer): integer;
VaR
FN: pvfn;
// It can also be defined directly here, for example: FN: function (para: integer): integer;
HM: hmodule;
Begin
HM: = loadlibrary ('test. dll ');
FN: = getprocaddress (HM, 'function1 ');
FN (2 );
Freelibrary (HM );
End;

Appendix:
Keylife rich notes
Author? : Unfamiliar
Title? : Delphi pointer understanding
Keyword: pointer
Category? : Personal zone
Confidentiality level? : Public
* ******************** Reference ***************** ********
In Delphi, the pointer function is very powerful and can be implemented in all the pointers implemented in C. In the above view, the Delphi pointer is not a weakness but a misunderstanding (or a thorough understanding of the pointer mechanism ).
Due to the limitations of Pascal, forced type conversion is required in many cases when Delphi pointers are used. delphi provides many pointer types, and you can easily customize your own pointer types.
One experience: to master a data type and use it flexibly, it is better to consider the type and name, instead of the number of bytes occupied by such variables. all types with the same number of bytes can be considered as the same type:-). Different types are provided only for the compiler to conveniently find errors. for example: integer, pointer, pchar, tsmallpoint or even array [0 .. 4] of char
You can use them as the same type (with this idea, you can achieve a lot Program Flexibility and code efficiency ). so what I do not understand is that pointers are not supported in Java (so I also think that it is absolutely impossible to write very efficient programs using Java, in addition, there will be a lot of work that can be done in one sentence in C/C ++/Delphi. in Java, a complicated process is required, consuming a lot of extra memory for the same purpose ). on the case, the solution in Delphi and C is no different based on your problem.
VaR
A: pointer; // or any other pointer type
Begin
Getmem (A, 10); // allocate 10 bytes
Pinteger (a) ^: = INTEGER (form1); // form1 (in fact, all classes in Delphi can be considered as pointers)
// Save it to a [0 .. 3]
// The difference between this and C is that pointer type conversion is required in Delphi,
// Mainly to "cheat" the compiler, so that the program cannot be compiled.
(Pchar (A) + sizeof (integer) ^: = 'B'; // save a character 'B' in a [4'
....
End;

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.