Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pascal (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Pointer types=== Pascal supports the use of [[pointer (computer programming)|pointer]]s: :<syntaxhighlight lang="pascal"> type pNode = ^Node; Node = record a : integer; b : char; c : pNode end; var NodePtr : pNode; IntPtr : ^integer; </syntaxhighlight> Here the variable ''NodePtr'' is a pointer to the data type ''Node'', a record. Pointers can be used before they are declared. This is a [[forward declaration]], an exception to the rule that things must be declared before they are used. To create a new record and assign the value ''10'' and character ''A'' to the fields ''a'' and ''b'' in the record, and to initialise the pointer ''c'' to the [[null pointer]] ("NIL" in Pascal), the statements would be: :<syntaxhighlight lang="pascal"> new(NodePtr); ... NodePtr^.a := 10; NodePtr^.b := 'A'; NodePtr^.c := nil; ... </syntaxhighlight> This could also be done using the <code>with</code> statement, as follows: <syntaxhighlight lang="pascal"> new(NodePtr); ... with NodePtr^ do begin a := 10; b := 'A'; c := nil end; ... </syntaxhighlight> Inside of the scope of the ''with'' statement, a and b refer to the subfields of the record pointer ''NodePtr'' and not to the record Node or the pointer type pNode. [[Linked list]]s, [[Stack (abstract data type)|stacks]] and [[Queue (abstract data type)|queues]] can be created by including a pointer type field (c) in the record. Unlike many languages that feature pointers, Pascal only allows pointers to reference dynamically created variables that are anonymous, and does not allow them to reference standard static or local variables. Pointers also must have an associated type, and a pointer to one type is not compatible with a pointer to another type (e.g. a pointer to a char is not compatible with a pointer to an integer). This helps eliminate the type security issues inherent with other pointer implementations, particularly those used for [[PL/I]] or [[C (Programming Language)|C]]. It also removes some risks caused by [[dangling pointers]], but the ability to dynamically deallocate referenced space by using the ''dispose'' function (which has the same effect as the ''free'' library function found in [[C (programming language)|C]]) means that the risk of dangling pointers has not been eliminated<ref name="Hoare.Sneeringer.Welsh.1977">J. Welsh, W. J. Sneeringer, and C. A. R. Hoare, "Ambiguities and Insecurities in Pascal", ''Software: Practice and Experience 7'', pp. 685β696 (1977)</ref> as it has in languages such as Java and C#, which provide [[Garbage collection (computer science)|automatic garbage collection]] (but which do not eliminate the related problem of [[memory leak]]s). Some of these restrictions can be lifted in newer dialects.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)