Difference between revisions of "Basic Pascal Tutorial/Chapter 5/Pointers"

From Lazarus wiki
Jump to navigationJump to search
Line 4: Line 4:
  
 
To declare a pointer data type, you must specify what it will point to. That data type is preceded with a carat (^). For example, if you are creating a pointer to an integer, you would use this code:
 
To declare a pointer data type, you must specify what it will point to. That data type is preceded with a carat (^). For example, if you are creating a pointer to an integer, you would use this code:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  PointerType <font color="#000000"><strong>=</strong></font> <font color="#000000"><strong>^</strong></font><font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
type
 +
  PointerType = ^integer;
 +
</delphi>
  
 
You can then, of course, declare variables to be of type PointerType.
 
You can then, of course, declare variables to be of type PointerType.
  
 
Before accessing a pointer, you block off an area in memory for that pointer to access. This is done with:
 
Before accessing a pointer, you block off an area in memory for that pointer to access. This is done with:
New (PointerVariable);
+
<delphi>
 +
New (PointerVariable);
 +
</delphi>
  
 
To access the data at the pointer's memory location, you add a carat after the variable name. For example, if PointerVariable was declared as type PointerType (from above), you can assign the memory location a value by using:
 
To access the data at the pointer's memory location, you add a carat after the variable name. For example, if PointerVariable was declared as type PointerType (from above), you can assign the memory location a value by using:
PointerVariable^ := 5;
+
<delphi>
 +
PointerVariable^ := 5;
 +
</delphi>
  
 
After you are done with the pointer, you must deallocate the memory space. Otherwise, each time the program is run, it will allocate more and more memory until your computer has no more. To deallocate the memory, you use the <tt>Dispose</tt> command:
 
After you are done with the pointer, you must deallocate the memory space. Otherwise, each time the program is run, it will allocate more and more memory until your computer has no more. To deallocate the memory, you use the <tt>Dispose</tt> command:
Dispose(PointerVariable);
+
<delphi>
 +
Dispose(PointerVariable);
 +
</delphi>
  
 
A pointer can be assigned to another pointer. However, note that since only the '''address''', not the value, is being copied, once you modify the data located at one pointer, the other pointer, when dereferenced, also yields modified data. Also, if you free (or deallocate) a pointer, the copied pointer now points to meaningless data.
 
A pointer can be assigned to another pointer. However, note that since only the '''address''', not the value, is being copied, once you modify the data located at one pointer, the other pointer, when dereferenced, also yields modified data. Also, if you free (or deallocate) a pointer, the copied pointer now points to meaningless data.
Line 23: Line 31:
  
 
A dynamic data structure, on the other hand, takes up only as much memory as is being used. What you do is to create a data type that points to a record. Then, the record has that pointer type as one of its fields. For example, stacks and queues can all be implemented using this data structure:
 
A dynamic data structure, on the other hand, takes up only as much memory as is being used. What you do is to create a data type that points to a record. Then, the record has that pointer type as one of its fields. For example, stacks and queues can all be implemented using this data structure:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  PointerType <font color="#000000"><strong>=</strong></font> <font color="#000000"><strong>^</strong></font>RecordType<font color="#000000"><strong>;</strong></font>
+
type
  RecordType <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>record</strong></font>
+
  PointerType = ^RecordType;
  data <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
  RecordType = record
  next <font color="#000000"><strong>:</strong></font> PointerType<font color="#000000"><strong>;</strong></font>
+
    data : integer;
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
  next : PointerType;
 +
end;
 +
</delphi>
  
 
Each element points to the next. The last record in the chain indicates that there is no next record by setting its next field to a value of <tt>nil</tt>.
 
Each element points to the next. The last record in the chain indicates that there is no next record by setting its next field to a value of <tt>nil</tt>.

Revision as of 16:13, 6 January 2010

5F - Pointers (author: Tao Yue, state: unchanged)

A pointer is a data type which holds a memory address. A pointer can be thought of as a reference to that memory address, while a variable accesses that memory address directly. If a variable is someone's phone number, then a pointer is the page and line number where it's listed in the phone book. To access the data stored at that memory address, you dereference the pointer.

To declare a pointer data type, you must specify what it will point to. That data type is preceded with a carat (^). For example, if you are creating a pointer to an integer, you would use this code: <delphi> type

 PointerType = ^integer;

</delphi>

You can then, of course, declare variables to be of type PointerType.

Before accessing a pointer, you block off an area in memory for that pointer to access. This is done with: <delphi> New (PointerVariable); </delphi>

To access the data at the pointer's memory location, you add a carat after the variable name. For example, if PointerVariable was declared as type PointerType (from above), you can assign the memory location a value by using: <delphi> PointerVariable^ := 5; </delphi>

After you are done with the pointer, you must deallocate the memory space. Otherwise, each time the program is run, it will allocate more and more memory until your computer has no more. To deallocate the memory, you use the Dispose command: <delphi> Dispose(PointerVariable); </delphi>

A pointer can be assigned to another pointer. However, note that since only the address, not the value, is being copied, once you modify the data located at one pointer, the other pointer, when dereferenced, also yields modified data. Also, if you free (or deallocate) a pointer, the copied pointer now points to meaningless data.

What is a pointer good for? Why can't you just use an integer in the examples above instead of a pointer to an integer? Well, the above is clearly a contrived example. The real power of pointers is that, in conjunction with records, it makes dynamically-sized data structures possible. If you need to store many items of one data type in order, you can use an array. However, your array has a predefined size. If you don't have a large enough size, you may not be able to accomodate all the data. If you have a huge array, you take up a lot of memory when sometimes that memory is not being used.

A dynamic data structure, on the other hand, takes up only as much memory as is being used. What you do is to create a data type that points to a record. Then, the record has that pointer type as one of its fields. For example, stacks and queues can all be implemented using this data structure: <delphi> type

 PointerType = ^RecordType;
 RecordType = record
   data : integer;
 next : PointerType;

end; </delphi>

Each element points to the next. The last record in the chain indicates that there is no next record by setting its next field to a value of nil.

previous contents next