Pointers
In Ada pointer types are called access types. Access types are subject of so-called accessibility checks, which in Ada are intended to prevent appearance of dangling pointers. (Though accessibility checks can be circumvented)
Create a pool specific access type for an integer
type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer (5);
A pool specific access type can be constrained to never be null. For such pointers the compiler can omit checks of being null upon dereferencing.
type Safe_Int_Access is not null access Integer;
General access types can deal with objects allocated in any pool as well as ones on the stack. For example, create a pointer to a stack-allocated integer
declare
type Int_Ptr is access all Integer;
Ref : Int_Ptr;
Var : aliased Integer := 3;
begin
Ref := Var'Access;
The attribute 'Access (and also 'Unchecked_Access) is used to get a pointer to the object. Note that the object has to be declared aliased when it has to be referenced by a pointer. General access types can also be constrained to exclude null:
type Safe_Int_Ptr is not null access all Integer;
Addresses
Ada does not provide pointer arithmetic, but does allow evaluation of the address
Var : Integer;
Var_Address : Address := Var'Address;
Addresses support operations of comparison, addition, and subtraction. Ada also supports conversion between address types and a predefined subtype of Integer named Integer_Address. This accommodates the conversion to a linear addressing of any hardware address scheme including address:offset used in the 8086 processor.
Ada allows the specification of a starting address for any object
-- Demonstrate the overlay of one object on another
A : Integer;
B : Integer;
for B'Address use A'Address;
-- A and B start at the same address
[edit] References
References in Ada are achieved through object renaming declarations. A renaming produces a new view to the object:
type Container is array (Positive range <>) of Element;
for I in Container'Range loop
declare
Item : Element renames Container (I);
begin
... -- Here Item is a reference to Container (I)
end;
end loop;
ALGOL 68
The following code creates a pointer to an INT variable:
INT var := 3;
REF INT pointer := var;
Access the integer variable through the pointer:
INT v = pointer; # sets v to the value of var (i.e. 3) #
REF INT(pointer) := 42; # sets var to 42 #
Change the pointer to refer to another object:
INT othervar;
pointer := othervar;
Change the pointer to not point to any object:
pointer := NIL; # 0 cannot be cast to NIL #
Get a pointer to the first element of an array:
[9]INT array;
pointer := array[LWB array];
There is no pointer arithmetic, eg no p +:=3
With references:
The following code creates a constant reference to an INT variable, effectively an alias:
REF INT alias = var;
Access the integer variable through the reference:
INT v2 = alias; # sets v2 to the value of var, that is, 3 #
alias := 42; # sets var to 42 #
Constand references cannot be changed to refer to other objects.
Pointers can be compared, but only for basic equality:
printf(($"alias "b("IS","ISNT")" var!"l$, alias IS var));
Output: alias IS var!
Get a reference to the first element of an array:
[9]INT array2;
REF INT ref3 = array2[LWB array2];
Changing the reference to refer to another object of the array is not possible.
ALGOL 68 also allows pointers to slices of rows and/or columns of arrays:
[9,9]INT sudoku;
REF [,]INT middle;
middle := sudoku[4:6,4:6];
This includes pointers to sliced character arrays:
[30]CHAR hay stack := "straw straw needle straw straw";
REF[]CHAR needle = hay stack[13:18];
needle[2:3] := "oo";
print((hay stack))
Output: straw straw noodle straw straw
C and C++
The following code creates a pointer to an int variable
int var = 3;
int* pointer = &var;
Access the integer variable through the pointer:
int v = *pointer; /* sets v to the value of var (i.e. 3) */
*pointer = 42; /* sets var to 42 */
Change the pointer to refer to another object
int othervar;
pointer = &othervar;
Change the pointer to not point to any object
pointer = NULL; /* needs having stddef.h included */
or
pointer = 0; /* actually any constant integer expression evaluating to 0 could be used, e.g. (1-1) will work as well */
or
pointer = (void*)0; /* C only, not allowed in C++ */
Get a pointer to the first element of an array:
int array[10];
pointer = array;
/* or alternatively: */
pointer = &array[0];
Move the pointer to another object in the array
pointer += 3; /* pointer now points to array[3] */
pointer -= 2; /* pointer now points to array[1] */
Access another object in the same array through the pointer
v = pointer[3]; /* accesses third-next object, i.e. array[4] */
v = pointer[-1]; /* accesses previous object, i.e. array[0] */
/* or alternatively */
v = *(pointer + 3); /* array[4] */
v = *(pointer - 1); /* array[0] */
[edit] C++
With pointers: See 'C' example above.
C++ specific alternative to "The following code creates a pointer to an int variable":
int* pointer2(&var);
With references:
The following code create a reference to an int variable:
int var = 3;
int& ref = var;
// or alternatively:
int& ref2(var);
Access the integer variable through the reference
int v = ref; // sets v to the value of var, that is, 3
ref = 42; // sets var to 42
References cannot be changed to refer to other objects, and cannot (legally) made to refer to no object.
Get a reference to the first element of an array:
int array[10];
int& ref3 = array[0];
Changing the reference to refer to another object of the array is not possible.
Accessing another object of the array through the reference:
v = (&ref)[3]; // read value of array[3]; however doing this is bad style
D
Grabbing variable address and placing it in a pointer
int var;
int* ptr = &var;
Depending on variable type, D will automatically pass either by value or reference by value: structs, statically sized arrays, and other primitives (int, char, etc...) by reference: classes, dynamically sized arrays, array slices
struct S{}
class C{}
void foo(S s){} // pass by value
void foo(C c){} // pass by reference
void foo(int i){} // pass by value
void foo(int[4] i){} // pass by value
void foo(int[] i){} // pass by reference
void foo(ref T t){} // pass by reference regardless of what type T really is
DISINI BUKAN HANYA TENTANG TEKHNIK INFORMATIKA TAPI BERBAGAI MACAM SEKMEN ContactPerson : 085759898986
Jumat, 15 Mei 2009
Belajar algoritma, Siapa Takut ??
Diposting oleh
TEKHNIK INFORMATIKA
di
Jumat, Mei 15, 2009
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar