Professional Documents
Culture Documents
Icp 14
Icp 14
Memory
! Physical ! Each
! Data
is stored in a physical byte (or series of bytes). can be accessed by using its physical address.
! Data
Pointers
! Pointers
Pointer Syntax
! The
are a data type that contain memory addresses. pointer can point to a variable of any type (eg a single byte, a string or an entire record). a pointer contains the address of the start of a data construct - pointers are the same size regardless of what they are pointing to. can be untyped or typed (ie what data type they are pointing to may be specified).
^ character
eg var pt1 : ^byte; writeln(pt1^);
^byte - a pointer that is pointing to a byte pt1^ - the data pointed to by pt1
eg
!A
! The
@ character
eg var myVar : byte; pt1 : ^byte; ...
! Because
! Pointers
pt1:=@myVar; writeln(pt1^);
Pointer Behaviour
! Remember! ! Pointers
do not contain data - they contain a reference to data. pointers can point at one data object thus changing the content of one will seemingly change the content of them all. Really only one copy of the data exists.
Heap is an area of memory that can be allocated (and deallocated) while a program is running (ie dynamically). allows programs to re-use memory (eg for arrays of indeterminate size).
! Several
! This
Advantages of Pointers
! Data
can be accessed without physically moving it in memory. ! Dynamic allocation makes much more efficient use of memory - allowing very large data constructs. ! Because pointers point to physical memory they can allow access to data anywhere. ! Some languages make much more extensive use of pointers than does Pascal - eg C and C++.
! It
is easy to accidentally modify data - especially if more than one pointer is pointing at the same data.
! Memory
! If
leaks
memory on the heap is not deallocated correctly, it may retain data even when the program terminates making this memory unavailable to other programs.