CynicRus
2015-02-16 03:06:43
Hey. Today I will try to explain how how to using a pointers in Lape. I hope - this article may be helpful for your scripting.
Part1:
Pointers in the World of Object Orientation:
[LEFT]In the past, especially in languages, pointers were a critical aspect of the language. They allowed direct access to memory, enabling complex data structures to be built and navigated. However, with the advent of Object Orientation, things have changed somewhat. For example, the TStringList class allows a list of strings to be built without the user needing to manage the storage. And hence, removing the need to use pointers to do this.Additionally, the Pascal language has also evolved to avoid dynamic storage management by coders - for example with the advent of dynamic arrays. You can now simply code a SetLength call to set or increase the size of such an array as your program runs. However, there are still some situations where pointers are valid in Lape.[/LEFT]
What are pointers?
[LEFT]Simply put, a pointer is a variable that holds the address of anything in memory.
To concrete this definition, keep in mind the following: everything used in an application is stored somewhere in the computer's memory. Because a pointer holds the address of another variable, it is said to point to that variable.
Most of the time pointers in Lape point to a specific type:[/LEFT]
[LEFT]The syntax to declare a pointer data type uses a caret (^) . In the code above iValue is an integer type variable and pIntValue is an integer type pointer. Since a pointer is nothing more than an address in memory, we must assign to it the location (address) of value stored in iValue integer variable. The @ operator returns the address of a variable (or a function or procedure as will be seen later in this article). Equivalent to the @ operator is the Addr function . Note that pIntValue's value is not 2001.
In the code above the pIntValue is a typed integer pointer. Good programming style is to use typed pointers as much as you can. The Pointer data type is a generic pointer type - represents a pointer to any data. [/LEFT]
Types of pointers:
[LEFT]Lape provides a number of typed pointer types, such as PChar, along with a generic, 'point to anything' type - the Pointer type. The nice thing about the typed pointers is that they work sensibly with the Inc and Dec functions. Incrementing an Pinteger pointer will add SizeOf(Integer) bytes to the pointer address so that it points to the next Integer variable in memory.The Pointer type is a dangerous one - it falls foul of Lape's normally tight type handling. Use it with care, or you will end up addressing the wrong memory.[/LEFT]
Part1:
Pointers in the World of Object Orientation:
[LEFT]In the past, especially in languages, pointers were a critical aspect of the language. They allowed direct access to memory, enabling complex data structures to be built and navigated. However, with the advent of Object Orientation, things have changed somewhat. For example, the TStringList class allows a list of strings to be built without the user needing to manage the storage. And hence, removing the need to use pointers to do this.Additionally, the Pascal language has also evolved to avoid dynamic storage management by coders - for example with the advent of dynamic arrays. You can now simply code a SetLength call to set or increase the size of such an array as your program runs. However, there are still some situations where pointers are valid in Lape.[/LEFT]
What are pointers?
[LEFT]Simply put, a pointer is a variable that holds the address of anything in memory.
To concrete this definition, keep in mind the following: everything used in an application is stored somewhere in the computer's memory. Because a pointer holds the address of another variable, it is said to point to that variable.
Most of the time pointers in Lape point to a specific type:[/LEFT]
var
iValue, j : integer;
pIntValue : ^integer;
begin
iValue := 2001;
pIntValue := @iValue;
...
j:= pIntValue^;
end; [LEFT]The syntax to declare a pointer data type uses a caret (^) . In the code above iValue is an integer type variable and pIntValue is an integer type pointer. Since a pointer is nothing more than an address in memory, we must assign to it the location (address) of value stored in iValue integer variable. The @ operator returns the address of a variable (or a function or procedure as will be seen later in this article). Equivalent to the @ operator is the Addr function . Note that pIntValue's value is not 2001.
In the code above the pIntValue is a typed integer pointer. Good programming style is to use typed pointers as much as you can. The Pointer data type is a generic pointer type - represents a pointer to any data. [/LEFT]
Types of pointers:
[LEFT]Lape provides a number of typed pointer types, such as PChar, along with a generic, 'point to anything' type - the Pointer type. The nice thing about the typed pointers is that they work sensibly with the Inc and Dec functions. Incrementing an Pinteger pointer will add SizeOf(Integer) bytes to the pointer address so that it points to the next Integer variable in memory.The Pointer type is a dangerous one - it falls foul of Lape's normally tight type handling. Use it with care, or you will end up addressing the wrong memory.[/LEFT]