Revision history for TutPointers


Revision [22358]

Last edited on 2018-08-16 14:56:21 by fxm [Reworded the ambiguous terms 'reference']
Additions:
Using pointers is a bit different. Imagine you have a little scrap of paper that will represent our pointer. Right now it is blank and doesn't point to anything. This undefined pointer can't be used until it is initialized. To initialize the pointer, write a 1 on it. Now our pointer is "pointing" to hole 1 in our egg carton. To put data (an egg) in hole 1, we look at our scrap of paper, match it to hole 1 and place the egg in the hole. To retrieve the egg we do just the opposite. We match our slip of paper to hole 1 and then grab the egg. All the putting and getting of the egg has to be done through the slip of paper and is called dereferencing the pointer. That is, we get to the data through the referring address contained in the pointer, the number 1. The pointer doesn't contain the data; it contains a referring address to the data.
Notice the ***** prefix on aptr. The * is the dereference operator. This is like matching the number on the slip of paper to the number on the hole in the egg carton. By using the * operator, we are able to get at the data (egg) contained in the hole pointed at by aptr.
Once we have allocated space for the string, we use the dereference operator * to load the data into the memory location.
We can now dereference the string in our type using the dereference operator.
Pointers can be confusing for the uninitiated, however they need not be if it is kept in mind that the pointer doesn't contain data, it simply points to some data. The pointer is a memory address, and you manipulate that data through the dereference operator *. It really isn't much different than a normal variable.
Deletions:
Using pointers is a bit different. Imagine you have a little scrap of paper that will represent our pointer. Right now it is blank and doesn't point to anything. This undefined pointer can't be used until it is initialized. To initialize the pointer, write a 1 on it. Now our pointer is "pointing" to hole 1 in our egg carton. To put data (an egg) in hole 1, we look at our scrap of paper, match it to hole 1 and place the egg in the hole. To retrieve the egg we do just the opposite. We match our slip of paper to hole 1 and then grab the egg. All the putting and getting of the egg has to be done through the slip of paper and is called dereferencing the pointer. That is, we get to the data through the reference contained in the pointer, the number 1. The pointer doesn't contain the data; it contains a reference to the data.
Notice the ***** prefix on aptr. The * is the reference operator. This is like matching the number on the slip of paper to the number on the hole in the egg carton. By using the * operator, we are able to get at the data (egg) contained in the hole pointed at by aptr.
Once we have allocated space for the string, we use the reference operator * to load the data into the memory location.
We can now reference the string in our type using the reference operator.
Pointers can be confusing for the uninitiated, however they need not be if it is kept in mind that the pointer doesn't contain data, it simply points to some data. The pointer is a memory address, and you manipulate that data through the reference operator *. It really isn't much different than a normal variable.


Revision [22231]

Edited on 2018-02-11 11:46:25 by JeffMarshall [escape camel-case words]
Additions:
In ""FreeBASIC"" we define a pointer using the [[KeyPgDim|Dim]] and [[KeyPgPtr|Ptr]] statements:
A major reason for adding pointers to ""FreeBASIC"" is that many external libraries require pointers to type structures and pointers to strings. For example, the Win32 API has many structures that must be filled out and then passed to a function through a pointer.
Another use of a pointer is in a [[KeyPgType|Type]] definition. [[KeyPgType|Type]] defs in ""FreeBASIC"" can only contain fixed length strings, but what if you don't know the length of a string until the program is running? A pointer can serve this purpose.
mytype.sptr = pSetString("Hello World From FreeBASIC!")
mytype.sptr = pSetString("Hello World From FreeBASIC!")
Deletions:
In FreeBasic we define a pointer using the [[KeyPgDim|Dim]] and [[KeyPgPtr|Ptr]] statements:
A major reason for adding pointers to FreeBasic is that many external libraries require pointers to type structures and pointers to strings. For example, the Win32 API has many structures that must be filled out and then passed to a function through a pointer.
Another use of a pointer is in a [[KeyPgType|Type]] definition. [[KeyPgType|Type]] defs in FreeBasic can only contain fixed length strings, but what if you don't know the length of a string until the program is running? A pointer can serve this purpose.
mytype.sptr = pSetString("Hello World From FreeBasic!")
mytype.sptr = pSetString("Hello World From FreeBasic!")


Revision [22190]

Edited on 2018-02-07 16:34:53 by sancho3 [escape camel-case words]
Additions:
//Last reviewed by ""sancho3"" on February 07, 2018//


Revision [21809]

Edited on 2017-08-10 08:57:20 by fxm [Updated examples for compatibility with "fb" dialect]
Deletions:
option explicit
option explicit


Revision [21454]

Edited on 2016-05-11 01:04:27 by DkLwikki [Update for 64bit port]
Additions:
A pointer is a 4-byte data type (on 32bit systems) or 8-byte data type (on 64bit systems) that holds an address to a memory location. A pointer doesn't contain data, it points to data once it has been initialized. An uninitialized pointer points to nothing and is undefined.
Deletions:
A pointer is a 4-byte data type that holds an address to a memory location. A pointer doesn't contain data, it points to data once it has been initialized. An uninitialized pointer points to nothing and is undefined.


Revision [20701]

Edited on 2016-02-10 16:15:31 by DkLwikki [Update link format]
Additions:
//Written by [[WikiRick|rdc]]//
In FreeBasic we define a pointer using the [[KeyPgDim|Dim]] and [[KeyPgPtr|Ptr]] statements:
Here we are using [[KeyPgAllocate|Allocate]] to set aside enough space in memory for an [[KeyPgInteger|Integer]] and loading the address of that space into aptr. The [[KeyPgSizeof|sizeof]] macro returns the size in bytes of the passed data type. You could use len instead of [[KeyPgSizeof|Sizeof]] (since .13b) if you prefer.
The [[KeyPgDeallocate|deallocate]] function frees the memory pointed at by aptr, and makes aptr undefined once again. This is like erasing the number on our slip of paper. If we were to use aptr after deallocating it, the program would crash.
Another use of a pointer is in a [[KeyPgType|Type]] definition. [[KeyPgType|Type]] defs in FreeBasic can only contain fixed length strings, but what if you don't know the length of a string until the program is running? A pointer can serve this purpose.
Here we define our type with a field sptr as [[KeyPgZstring|zstring]] [[KeyPgPtr|ptr]]. Zstrings are null terminated strings and are used by many external libraries and are designed for dynamic allocations. Once we define our type we create an instance of it with the [[KeyPgDim|dim]] statement:
We then call our function pSetString to get the address of the variable length string we want in our [[KeyPgType|type]] def.
The function pSetString uses a temporary [[KeyPgZstring|zstring]] sz, to [[KeyPgAllocate|allocate]] space for the passed string parameter s. Because a [[KeyPgZstring|zstring]] is a null terminated string, we must add 1 to the length of s for the null terminator in the [[KeyPgAllocate|allocate]] function.
Deletions:
//Written by [[WikiRick rdc]]//
In FreeBasic we define a pointer using the [[KeyPgDim Dim]] and [[KeyPgPtr Ptr]] statements:
Here we are using [[KeyPgAllocate Allocate]] to set aside enough space in memory for an [[KeyPgInteger Integer]] and loading the address of that space into aptr. The [[KeyPgSizeof sizeof]] macro returns the size in bytes of the passed data type. You could use len instead of [[KeyPgSizeof Sizeof]] (since .13b) if you prefer.
The [[KeyPgDeallocate deallocate]] function frees the memory pointed at by aptr, and makes aptr undefined once again. This is like erasing the number on our slip of paper. If we were to use aptr after deallocating it, the program would crash.
Another use of a pointer is in a [[KeyPgType Type]] definition. [[KeyPgType Type]] defs in FreeBasic can only contain fixed length strings, but what if you don't know the length of a string until the program is running? A pointer can serve this purpose.
Here we define our type with a field sptr as [[KeyPgZstring zstring]] [[KeyPgPtr ptr]]. Zstrings are null terminated strings and are used by many external libraries and are designed for dynamic allocations. Once we define our type we create an instance of it with the [[KeyPgDim dim]] statement:
We then call our function pSetString to get the address of the variable length string we want in our [[KeyPgType type]] def.
The function pSetString uses a temporary [[KeyPgZstring zstring]] sz, to [[KeyPgAllocate allocate]] space for the passed string parameter s. Because a [[KeyPgZstring zstring]] is a null terminated string, we must add 1 to the length of s for the null terminator in the [[KeyPgAllocate allocate]] function.


Revision [11869]

The oldest known version of this page was created on 2007-12-07 07:07:15 by HaRmonv [Update link format]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode