Pointer

From Academic Kids

This article is about the computer data type. For other meanings of pointer, see pointer (disambiguation).

In computer science, a pointer is a programming language datatype whose value is used to refer to ("points to") another value stored elsewhere in the computer memory. Obtaining the value that a pointer refers to is called dereferencing the pointer. A pointer is a simple implementation of the general reference datatype, although it is quite different from the facility referred to as a reference in C++.

Contents

Everyday Analogy

We regularly use pointers in everyday life when we use telephone numbers.

[Note : 
 a)Telephone no.s give 2-way access whereas a pointer gives only 1-way access.
 b)Usually the no. of links is 1 to 3 in an access path. 
]
  Tom ---> Jerry
  Tom ---> 1 ---> Jerry 
  Tom ---> 1 ---> 2 ---> .... ---> N ---> Jerry

Here Tom calls Jerry by

a)Getting the contact no. directly or indirectly from 'a friend-of-a-friend-of-a-friend'.
b)Dialling the number.

Here the intermediate links of the chain can change the destination similar to an operator diverting your call to the concerned person in an organisation. Say Tom wants some info (from Jerry), but N on the path knows that Mortimer is a better source of info and gives Mortimer's no. instead.

a)Original path:
Tom ---> 1 ---> 2 --->...N --->Jerry
b)Redirection:
Tom ---> 1 ---> 2 --->...N ---> Mortimer

This is similar to shifting tracks for a train, we can easily divert the train to whatever track by using the shunting mechanism.

Architectural roots

Pointers are a very thin abstraction on top of the addressing capabilities provided by most modern architectures. In the simplest scheme, an address, or a numeric index, is assigned to each unit of memory in the system, where the unit is typically either a byte or a word, effectively transforming all of memory into a very large array. Then, if we have an address, the system provides an operation to retrieve the value stored in the memory unit at that address. Pointers are datatypes which hold addresses. See reference (computer science).

In the usual case, a pointer is large enough to hold more different addresses than there are units of memory in the system. This introduces the possibility that a program may attempt to access an address which corresponds to no unit of memory, called a segmentation fault. On the other hand, some systems have more units of memory than there are addresses. In this case, a more complex scheme such as memory segmentation or paging is employed to use different parts of the memory at different times.

In order to provide a consistent interface, some architectures provide memory-mapped I/O, which allows some addresses to refer to units of memory while others refer to device registers of other devices in the computer. There are analogous concepts such as file offsets, array indices, and remote object references that serve some of the same purposes as addresses for other types of objects.

Uses

Pointers, which are directly supported without restrictions in C, C++, and most assembly languages, are primarily used for constructing references, which in turn are fundamental in constructing nearly all data structures, as well as in passing data between different parts of a program.

When dealing with arrays, the critical lookup operation typically involves a stage called address calculation which involves constructing a pointer to the desired data element in the array. In other data structures, such as linked lists, pointers are used as references to explicitly tie one piece of the structure to another.

Pointers are used to pass parameters by reference. This will be useful when we want the callee fuction (fuction which is called) modifications to parameters to be visible to caller function. This is also useful for returning multiple values from a fuction.

Typed pointers and casting

In many languages, pointers have the additional restriction that the object they point to has a specific type. For example, a pointer may be declared to point to an integer; the language will then attempt to prevent the programmer from pointing it to objects which are not integers, such as floating-point numbers, eliminating some errors.

However, few languages strictly enforce pointer types, because programmers often run into situations where they want to treat an object of one type as though it were of another type. For these cases, it is possible to typecast, or cast, the pointer. Some casts are always safe, while other casts are dangerous, possibly resulting in incorrect behavior later on. Although it's impossible in general to determine at compile-time which of these casts are safe, some languages store run-time type information which can be used to confirm that these dangerous casts are valid at runtime. Other languages merely accept a conservative approximation of safe casts, or none at all.

Making pointers safer

Because pointers are so close to the hardware, they enable a variety of programming errors. However, the power they provide is so great that it's difficult to do anything useful without them. To help deal with their problems, many languages have created objects that have some of the useful features of pointers, while avoiding some of their pitfalls.

One major problem with pointers is that, as long as they can be directly manipulated as a number, they can be made to point to unused addresses or to data which is being used for other purposes. Many languages, including most functional programming languages and recent imperative languages like Java, replace pointers with references, which can only be used to refer to objects and not manipulated as numbers, preventing this type of error. Array indexing is handled as a special case.

Before any address has been assigned to it, a pointer is called a wild pointer. Any attempt to use such uninitialized pointers can cause unexpected behaviour, either because the initial address is not a valid address, or because using it may damage the runtime system and other unrelated parts of the program.

In systems with explicit memory allocation, it's possible to create a "dangling" pointer, by deallocating the memory region it points into. This type of pointer is dangerous and subtle, because a deallocated memory region looks the same as it did before, but can be reused at any time by unrelated code. Languages with garbage collection prevent this type of error.

Some languages, like C++, support smart pointers, which use a simple form of reference counting to help track allocation of dynamic memory in addition to acting as a reference. In the absence of reference cycles, where an object refers to itself indirectly through a sequence of smart pointers, these eliminate the possibility of use of dangling pointers and memory leaks.

The null pointer

A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object. Null pointers are used routinely, particularly in C and C++, to represent exceptional conditions such as the lack of a successor to the last element of a linked list, while maintaining a consistent structure for the list nodes. This use of null pointers can be compared to the use of null values in relational databases. In C, each pointer type has its own null value, and sometimes they have different representations.

Because it refers to nothing, an attempt to dereference a null pointer causes a run-time error that usually terminates the program immediately. In safe languages a possibly null pointer can be replaced with a tagged union which enforces explicit handling of the exceptional case; in fact, a possibly-null pointer can be seen as a tagged union with a computed tag.

Pointer support in various programming languages

Ada

Ada is a strongly typed language where all pointers are typed and only safe type conversions are permitted. All pointers are by default initialized to null, and any attempt to access data through a null pointer causes an exception to be raised. Pointers in Ada are called access types. Ada-83 did not permit arithmetic on access types (although many compiler vendors provided for it as a non-standard feature), but Ada-95 supports "safe" arithmetic on access types via the package System.Storage_Elements.

C

In C, pointers are variables that store addresses and can be null. Each pointer has a type it points to, but one can freely cast between pointer types. A special pointer type called the "void pointer" points to an object of unknown type. The address can be directly manipulated by casting a pointer to and from an integer. Pointer arithmetic is unrestricted; adding or subtracting from a pointer moves it by a multiple of the size of the datatype it points to.

C++

C++ is a derivative of C which fully supports C pointers and C typecasting. It also supports a new group of typecasting operators to help catch some unintended dangerous casts at compile-time. The C++ standard library also provides autoptr, a sort of smart pointer which can be used in some situations as a safe alternative to primitive C pointers. C++ also supports another form of reference, quite different from a pointer, called simply a reference or reference type.

D

D is a derivative of C and C++ which fully supports C pointers and C typecasting. But D also offers numerous constructs such as foreach loops, out function parameters, reference types, and advanced array handling which replace pointers for most routine programming tasks.

Fortran

A Fortran pointer is best thought of as an "alias" of another object, such as a scalar variable or a row or column of an array. A Fortran pointer is thus not implemented as just an address. There is no syntax to "deference" the pointer or manipulate the contents of the descriptor directly. In most cases, a pointer refers to its target. A pointer can be null, indicating it has no target. As in other languages, pointers facilitate the processing of dynamic structures, such as linked lists, queues, and trees.

Modula 2

Pointers are implemented very much as in Pascal, as are VAR parameters in procedure calls. Modula 2 is even more strongly typed than Pascal, with fewer ways to escape the type system. Some of the variants of Modula 2 (such as Modula-3) include garbage collection.

Oberon

Much as with Modula-2, pointers are available. There are still fewer ways to evade the type system and so Oberon and its variants are still safer with respect to pointers than Modula-2 or its variants. As with Modula-3, garbage collection is a part of the language specification.

Pascal

Pascal implements pointers in a straightforward, limited, and relatively safe way. It helps catch mistakes made to be by people who are new to programming, like dereferencing a pointer into the wrong datatype; however, a pointer can be cast from one pointer type to another. Pointer arithmetic is unrestricted; adding or subtracting from a pointer moves it by that number of bytes in either direction, but using the Inc or Dec standard procedures on it moves it by the size of the datatype it is declared to point to. Trying to dereference a null pointer, named nil in Pascal, or a pointer referencing unallocated memory, raises an exception in protected mode. Parameters may be passed using pointers (as VAR parameters) but are automatically handled by the runtime system.

ML Family

In Standard ML and O'Caml (and many other functional languages), most values are persistent: they cannot be modified by assignment to a pointer. Assignable "reference cells" serve the unavoidable purposes of pointers in imperative languages, and make the capability to be modified explicit. Such reference cells can hold any value, and so are given the polymorphic type α ref, where α is to be replaced with the type of value pointed to. To preserve safety and efficient implementations, references cannot be type-cast in ML, nor can pointer arithmetic be performed. It is important to note that in the functional paradigm, many structures that would be represented using pointers in a language like C are represented using other facilities, such as the powerful algebraic datatype mechanism. The programmer is then able to enjoy certain properties (such as the guarantee of immutability) while programming, even though the compiler often uses machine pointers "under the hood".

See also

External links

  • Pointer Fun With Binky (http://cslibrary.stanford.edu/104/) Introduction to pointers in a 3 minute educational video - Stanford Computer Science Education Library (this link has crashed some browsers -- use with caution)de:Zeiger (Informatik)

it:puntatore ja:ポインタ ko:포인터 (프로그래밍) nl:pointer pl:Wskaźnik

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools