Citizendia
Your Ad Here

In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Computer science (or computing science) is the study and the Science of the theoretical foundations of Information and Computation and their A programming language is an Artificial language that can be used to write programs which control the behavior of a machine particularly a Computer. A data type in Programming languages is an attribute of a datum which tells the computer (and the programmer something about the kind of datum it is In Computer science, a memory address is an identifier for a memory location at which a Computer program or a hardware device can store a piece of data Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). In Computer science, a reference is an object containing information which refers to data stored elsewhere as opposed to containing the data itself In the C++ Programming language, a reference is a simple reference datatype that is less powerful but safer than the Pointer type inherited from Pointers to data improve performance for repetitive operations such as traversing string and tree structures, and pointers to functions are used for binding methods in Object-oriented programming and run-time linking to Dynamic Link Libraries (DLLs). In Computer programming and some branches of Mathematics, a string is an ordered Sequence of Symbols. In Computer science, a tree is a widely-used Data structure that emulates a Tree structure with a set of linked nodes A function pointer is a type of Pointer in C, C++, D, and other C-like programming languages In Programming languages name binding is the association of values with Identifiers An identifier bound to a value is said to reference In Object-oriented programming, the term method refers to a Subroutine that is exclusively associated either with a class (called class methods Object-oriented programming (OOP is a Programming paradigm that uses " objects " and their interactions to design applications and computer programs Dynamic-link library (also written without the hyphen or DLL, is Microsoft 's implementation of the Shared library concept in the Microsoft Windows

While "pointer" has been used to refer to references in general, it more properly applies to data structures whose interface explicitly allows the pointer to be manipulated as a memory address. Because pointers allow largely unprotected access to memory addresses, there are risks associated with using them. For general information about references, see reference (computer science). In Computer science, a reference is an object containing information which refers to data stored elsewhere as opposed to containing the data itself

Contents

Pointers in data structures

When setting up data structures like lists, queues and trees, it is necessary to have pointers to help manage the way in which the structure is implemented and controlled. A data structure in Computer science is a way of storing Data in a computer so that it can be used efficiently In Computer science, a list is an ordered collection of entities / Items In the context of Object-oriented programming languages A queue (pronounced /kjuː/ is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only operations on the collection Typical examples of pointers would be start pointers, end pointers, or stack pointers.

Architectural roots

Pointers are a very thin abstraction on top of the addressing capabilities provided by most modern architectures. In Computer science, abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time The software architecture of a program or computing system is the structure or structures of the system, which comprise software components the externally visible properties 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. In Computer science, a memory address is an identifier for a memory location at which a Computer program or a hardware device can store a piece of data This is referring to Index in the context of Information Technology A byte (pronounced "bite" baɪt is the basic unit of measurement of information storage in Computer science. In Computing, " word " is a term for the natural unit of data used by a particular computer design In Computer science an array is a Data structure consisting of a group of elements that are accessed by indexing. Then, if we have an address, the system provides an operation to retrieve the value stored in the memory unit at that address.

In the usual case, a pointer is large enough to hold more 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, either because not enough memory is installed or the architecture does not support such addresses. The first case may, in certain platforms as the Intel x86 architecture, be called a segmentation fault (segfault). See also X86 assembly language The generic term x86 refers to the most commercially successful Instruction set architecture in the history of Personal A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of Computer software. The second case is possible in the current implementation of AMD64, where pointers are 64 bit long and addresses only extend to 48 bits. x86-64 is a Superset of the x86 instruction set architecture. There, pointers must conform to certain rules (canonical addresses), so if a noncanonical pointer is dereferenced, the processor raises a general protection fault. A General Protection Fault ( GPF) in the Intel X86 and AMD X86-64 architectures and other unrelated architectures is a

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 computing memory segmentation is one of the most common ways to achieve Memory protection; another common one is Paging. In Computer Operating systems that have their Main memory divided into pages, paging (sometimes called swapping) is a transfer The last incarnations of the x86 architecture support up to 36 bits of physical memory addresses, which were mapped to the 32-bit linear address space through the PAE paging mechanism. In Computing, Physical Address Extension ( PAE) refers to a feature of X86 and X86-64 processors that allows more than 4 Gigabytes ( Thus, only 1/16 of the possible total memory may be accessed at a time. Another example in the same computer family was the 16-bit protected mode of the 80286 processor, which, though supporting only 16 MiB of physical memory, could access up to 1 GiB of virtual memory, but the combination of 16-bit address and segment registers made accessing more than 64 KiB in one data structure cumbersome. In computing protected mode, also called protected virtual address mode, is an operational mode of X86 -compatible Central processing units (CPU The Intel 286, introduced on February 1, 1982, (originally named 80286, and also called iAPX 286 in the programmer's manual Some restrictions of ANSI pointer arithmetic may have been due to the segmented memory models of this processor family.

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. Memory-mapped I/O ( MMIO) and port I/O (also called port-mapped I/O or PMIO) are two complementary methods of performing Input/output A Device Register is the view any device presents to a Programmer. 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. In Computer science an array is a Data structure consisting of a group of elements that are accessed by indexing.

Uses

Pointers are directly supported without restrictions in languages such as C, C++, Pascal and most assembly languages. tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. Pascal is an influential imperative and procedural Programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small See the terminology section below for information regarding inconsistent use of the terms assembly and assembler They are primarily used for constructing references, which in turn are fundamental to constructing nearly all data structures, as well as in passing data between different parts of a program. In Computer science, a reference is an object containing information which refers to data stored elsewhere as opposed to containing the data itself A data structure in Computer science is a way of storing Data in a computer so that it can be used efficiently

In functional programming languages that rely heavily on lists, pointers and references are managed abstractly by the language using internal constructs like cons. In Computer programming, cons (ˈkɒnz or /ˈkɒns/ is a fundamental function in all dialects of the Lisp programming language.

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 Computer science an array is a Data structure consisting of a group of elements that are accessed by indexing. 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 is useful if we want a function's modifications to a parameter to be visible to the function's caller. This is also useful for returning multiple values from a function.

C pointers

The basic syntax to define a pointer is

int *money;

This declares money as a pointer to an integer. Since the contents of memory are not guaranteed to be of any specific value in C, care must be taken to ensure that the address that money points to is valid. This is why it is suggested to initialize the pointer to NULL

int *money = NULL;

If a NULL pointer is dereferenced then a runtime error will occur and execution will stop likely with a segmentation fault.

Once a pointer has been declared then, perhaps, the next logical step is to point it at something

int a = 5;
int *money = NULL;
 
money = &a;

This assigns the value of money to be the address of a. For example, if a is stored at memory location of 0x8130 then the value of money will be 0x8130 after the assignment. To dereference the pointer, an asterisk is used again

*money = 8;

This says to take the contents of money (which is 0x8130), go to that address in memory and set its value to 8. If a were then accessed then its value will be 8.

This example may be more clear if memory were examined directly. Assume that a is located at address 0x8130 in memory and money at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits wide. The following is what would be in memory after the following code snippet were executed

int a = 5;
int *money = NULL;
Address Contents
0x8130 0x00000005
0x8134 0x00000000

(The NULL pointer shown here is 0x00000000. ) By assigning the address of a to money

money = &a;

yields the following memory values

Address Contents
0x8130 0x00000005
0x8134 0x00008130

Then by dereferencing money by doing

*money = 8;

the computer will take the contents of money (which is 0x8130), go to that address, and assign 8 to that location yielding the following memory.

Address Contents
0x8130 0x00000008
0x8134 0x00008130

Clearly, accessing a will yield the value of 8 because the previous instruction modified the contents of a by way of the pointer money.

C arrays

Taking C pointers to the next step is the array.

In C, array indexing is formally defined in terms of pointer arithmetic; that is, the language specification requires that array[i] be equivalent to *(array + i). Thus in C, arrays can be thought of as pointers to consecutive areas of memory, and the syntax for accessing arrays is identical for that which can be used to dereference pointers. For example, an array array can be declared and used in the following manner:

int array[5];      /* Declares 5 contiguous integers */
int *ptr = array;  /* Arrays can be used as pointers */
ptr[0] = 1;        /* Pointers can be indexed with array syntax */
*(array + 1) = 2;  /* Arrays can be dereferenced with pointer syntax */

This allocates a block of five integers and declares array as a pointer to this block. Another common use of pointers is to point to dynamically allocated memory from malloc which returns a consecutive block of memory of no less than the requested size that can be used as an array. In Computing, malloc is a Subroutine provided in the C and C++ programming language 's standard libraries for performing

While most operators on arrays and pointers are equivalent, it is important to note that the sizeof operator will differ. In this example, sizeof(array) will evaluate to 5*sizeof(int) (the size of the array), while sizeof(ptr) will evaluate to sizeof(int*), the size of the pointer itself.

Default values of an array can be declared like:

int array[5] = {2,4,3,1,5};

If you assume that array is located in memory starting at address 0x1000 on a 32-bit little-endian machine then memory will contain the following:

0 1 2 3
1000 02 00 00 00
1004 04 00 00 00
1008 03 00 00 00
100C 01 00 00 00
1010 05 00 00 00

Represented here are five integers: 2, 4, 3, 1, and 5. These five integers occupy 32 bits (4 bytes) each with the least-significant byte stored first (this is a little-endian architecture) and are stored consecutively starting at address 0x1000.

The syntax for C with pointers is:

The last example is how to access the contents of array. Breaking it down:

E. g. array[3] is synonymous with *(array+3), meaning *(0x1000 + 3*sizeof(int)), which says "dereference the value stored at 0x100C", in this case 0x0001.

C linked list

Below is an example of the definition of a linked list in C. In Computer science, a linked list is one of the fundamental Data structures and can be used to implement other data structures

/* the empty linked list is
 * represented by NULL or some
 * other signal value */
#define EMPTY_LIST NULL
 
struct link {
    /* the data of this link */
    void *data;
    /* the next link; EMPTY_LIST if this is the last link */
    struct link *next;
};

Note that this pointer-recursive definition is essentially the same as the reference-recursive definition from the Haskell programming language:

data Link a = Nil
            | Cons a (Link a)

Nil is the empty list, and Cons a (Link a) is a cons cell of type a with another link also of type a. Haskell is a standardized Purely functional Programming language with non-strict semantics, named after the Logician Haskell Curry In Computer programming, cons (ˈkɒnz or /ˈkɒns/ is a fundamental function in all dialects of the Lisp programming language.

The definition with references, however, is type-checked and doesn't use potentially confusing signal values. For this reason, data structures in C are usually dealt with via wrapper functions, which are carefully checked for correctness.

Pass by reference

Pointers can be used to pass variables by reference, allowing their value to be changed. For example:

void not_alter(int n) {
    n = 360;
}
 
void alter(int *n) {
    *n = 120;
}
 
void func(void) {
    int x = 24;
 
    not_alter(x);
 
    /* x still equal to 24 */
 
    alter(&x);
 
    /* x now equal to 120 */
}

Memory-mapped hardware

On some computing architectures, pointers can be used to directly manipulate memory or memory-mapped devices.

Assigning addresses to pointers is an invaluable tool when programming microcontrollers. A microcontroller (also MCU or µC is a functional Computer system-on-a- chip. Below is a simple example declaring a pointer of type int and initialising it to a hexadecimal address in this example the constant 0x7FFF:

int *hardware_address = (int *)0x7FFF;

In the mid 80s, using the BIOS to access the video capabilities of PCs was slow. In Mathematics and Computer science, hexadecimal (also base -, hexa, or hex) is a Numeral system with a In Computing, the BIOS (ˈbaɪoʊs Applications that were display-intensive typically used to access CGA video memory directly by casting the hexadecimal constant 0xB8000000 to a pointer to an array of 80 unsigned 16-bit int values. The Color Graphics Adapter ( CGA) originally also called the Color/Graphics Adapter or IBM Color/Graphics Monitor Adapter In Mathematics and Computer science, hexadecimal (also base -, hexa, or hex) is a Numeral system with a Each value consisted of an ASCII code in the low byte, and a colour in the high byte. American Standard Code for Information Interchange ( ASCII) Thus, to put the letter 'A' at row 5, column 2 in bright white on blue, one would write code like the following:

#define VID ((unsigned (*)[80])0xB8000000)
 
void foo() {
    VID[4][1] = 0x1F00 | 'A';
}

Typed pointers and casting

In many languages, pointers have the additional restriction that the object they point to has a specific type. A data type in Programming languages is an attribute of a datum which tells the computer (and the programmer something about the kind of datum it is 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. The integers (from the Latin integer, literally "untouched" hence "whole" the word entire comes from the same origin but via French In Computing, floating point describes a system for numerical representation in which a string of digits (or Bits represents a Real number.

For example, in C

int *money;
char *bags;

money would be an integer pointer and bags would be a char pointer. The following would yield a compiler warning of "assignment from incompatible pointer type" under GCC

bags = money;

because money and bags were declared with different types. The GNU Compiler Collection (usually shortened to GCC) is a set of Compilers produced for various Programming languages by the GNU Project To suppress the compiler warning, it must be made explicit that you do indeed wish to make the assignment by typecasting it

bags = (char *)money;

which says to cast the integer pointer of money to a char pointer and assign to bags. In Computer science, type conversion or typecasting refers to changing an entity of one Data type into another

In languages that allow pointer arithmetic, arithmetic on pointers takes into account the size of the type. For example, adding an integer number to a pointer produces another pointer that points to an address that is higher by that number times the size of the type. This allows us to easily compute the address of elements of an array of a given type, as was shown in the C arrays example above. When a pointer of one type is cast to another type of a different size, the programmer should expect that pointer arithmetic will be calculated differently. In C, for example, if the money array starts at 0x2000 and sizeof(int) is 4 bytes whereas sizeof(char) is 2 bytes, then (money+1) will point to 0x2004 but (bags+1) will point to 0x2002. Other risks of casting include loss of data when "wide" data is written to "narrow" locations (e. g. bags[0]=65537;), unexpected results when bit-shifting values, and comparison problems, especially with signed vs unsigned values. In Computer programming, a bitwise operation operates on one or two Bit patterns or binary numerals at the level of their individual Bits On most

Although it's impossible in general to determine at compile-time which casts are safe, some languages store run-time type information which can be used to confirm that these dangerous casts are valid at runtime. In programming RTTI ( Run-Time Type Information, or Run-Time Type Identification) refers to a C++ system that keeps information about an object's Other languages merely accept a conservative approximation of safe casts, or none at all.

Making pointers safer

Because pointers allow a program to access objects that are not explicitly declared beforehand, they enable a variety of programming errors. The word error has different meanings and usages relative to how it is conceptually applied However, the power they provide is so great that it can be difficult to do some programming tasks 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. In Software engineering, an anti-pattern (or antipattern) is a Design pattern that appears obvious but is ineffective or far from optimal in practice

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 a more opaque type of reference, typically referred to as simply a reference, which can only be used to refer to objects and not manipulated as numbers, preventing this type of error. In Computer science, functional programming is a Programming paradigm that treats Computation as the evaluation of mathematical functions and Array indexing is handled as a special case.

A pointer which does not have any address assigned to it is called a wild pointer. Dangling pointers and wild pointers in Computer programming are pointers that do not point to a valid object of the appropriate type Any attempt to use such uninitialized pointers can cause unexpected behaviour, either because the initial value 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. Dangling pointers and wild pointers in Computer programming are pointers that do not point to a valid object of the appropriate type This type of pointer is dangerous and subtle because a deallocated memory region may contain the same data as it did before it was deallocated but may be then reallocated and overwritten by unrelated code, unknown to the earlier code. Languages with garbage collection prevent this type of error. In Computer science, garbage collection ( GC) is a form of automatic Memory management.

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 computer science a smart pointer is an Abstract data type that simulates a Pointer while providing additional features such as automatic garbage collection In Computer science, reference counting is a technique of storing the number of references pointers or handles to a resource such as an object or block of memory In the absence of reference cycles, where an object refers to itself indirectly through a sequence of smart pointers, these eliminate the possibility of dangling pointers and memory leaks. Delphi strings support reference counting natively.

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 has several meanings in Computer programming.;Null pointer or null reference Null is a special Pointer value (or other kind of object reference Null pointers are used routinely, particularly in C and C++ where the compile-time constant NULL is used, to represent 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. In Computer science, a linked list is one of the fundamental Data structures and can be used to implement other data structures This use of null pointers can be compared to the use of null values in relational databases and to the “Nothing” value in the “Maybe” monad. A relational database is a Database that groups data using common attributes found in the data set In Functional programming, a monad is a kind of Abstract data type used to represent Computations (instead of data in the Domain model)

Because it does not refer to a meaningful object, an attempt to dereference a null pointer usually causes a run-time error that, if unhandled, terminates the program immediately. In the case of C, execution halts with a segmentation fault because the literal address of NULL is never allocated to a running program. In Java, access to a null reference triggers a NullPointerException, which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur. 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. In Computer science, a tagged union, also called a variant variant record discriminated union, or Disjoint union, is a Data structure used

In C and C++ programming, two null pointers are guaranteed to compare equal; ANSI C guarantees that any NULL pointer will be equal to 0 in a comparison with an integer type. ANSI C is the standard published by the American National Standards Institute (ANSI for the C programming language.

A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to any valid pointer, whereas depending on the language and implementation an uninitialized pointer might have either an indeterminate (random or meaningless) value or might be initialised to an initial constant (possibly but not necessarily NULL).

In most C programming environments malloc returns a NULL pointer if it is unable to allocate the memory region requested, which notifies the caller that there is insufficient memory available. In Computing, malloc is a Subroutine provided in the C and C++ programming language 's standard libraries for performing However, some implementations of malloc allow malloc(0) with the return of a NULL pointer and instead indicate failure by both returning NULL and setting errno to an appropriate value. TemplateC_Standard_library --> errnoh is a Header file in the standard library of C programming language.

Computer systems based on a tagged architecture are able to distinguish in hardware between a NULL dereference and a legitimate attempt to access a word or structure at address zero. In Computer science, a tagged architecture is a particular type of Computer architecture where every word of memory constitutes a Tagged union

In some programming language environments (at least one proprietary Lisp implementation, for example) the value used as the null pointer (called nil in Lisp) may actually be a pointer to a block of internal data useful to the implementation (but not explicitly reachable from user programs), thus allowing the same register to be used as a useful constant and a quick way of accessing implementation internals. This is known as the nil vector.

Double indirection

In C, it is possible to have a pointer point at another pointer. Although a higher number of pointer dereferences will add a performance penalty, this can make manipulating certain data structures particularly neat and elegant. A data structure in Computer science is a way of storing Data in a computer so that it can be used efficiently For instance, consider this code to insert an item into a simple linked list:

struct element {
        struct element *next;
        int value;
};
 
struct element *head = NULL;
 
void insert(struct element *item) {
        struct element **p;
        for(p = &head; *p != NULL; p = &(*p)->next) {
                if(item->value <= (*p)->value) {
                        break;
                }
        }
        item->next = *p;
        *p = item;
}

Wild pointers

Wild pointers are pointers that have not been initialized (that is, set to point to a valid address) and may make a program crash or behave oddly. In Computer science, a linked list is one of the fundamental Data structures and can be used to implement other data structures In the Pascal or C programming languages, pointers that are not specifically initialized may point to unpredictable addresses in memory. Pascal is an influential imperative and procedural Programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured

The following example code shows a wild pointer:

int func(void)
{
    char *p1 = malloc(sizeof(char)); /* (undefined) value of some place on the heap */
    char *p2;       /* wild (uninitialized) pointer */
    *p1 = 'a';      /* This is OK */
    *p2 = 'b';      /* ERROR: the result is undefined, and may lead to program crash */
}

Here, p2 may point to anywhere in memory, so performing the assignment *p2 = 'b' will corrupt an unknown area of memory that may contain sensitive data.

Note that in C and derived languages static variables without an initializer is initialized to zero on the program's start. Thus, the example above will dereference a NULL pointer which will lead to a segmentation fault.

Support in various programming languages

A number of languages support some type of pointer, although some are more restricted than others. If a pointer is significantly abstracted, such that it can no longer be manipulated as an address, the resulting data structure is no longer a pointer; see the more general reference article for more discussion of these. In Computer science, a reference is an object containing information which refers to data stored elsewhere as opposed to containing the data itself

Ada

Ada is a strongly typed language where all pointers are typed and only safe type conversions are permitted. Ada is a structured, Statically typed, imperative, and object-oriented high-level computer Programming language All pointers are by default initialized to null, and any attempt to access data through a null pointer causes an exception to be raised. Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of a condition that changes the normal flow of execution 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.

BASIC

BASIC does not support pointers. In Computer programming, BASIC (an Acronym for Beginner's All-purpose Symbolic Instruction Code) is a family of High-level programming languages Some dialects of BASIC, including FreeBASIC, have exhaustive pointer implementations, however. In Computer programming, BASIC (an Acronym for Beginner's All-purpose Symbolic Instruction Code) is a family of High-level programming languages FreeBASIC is a free / Open source ( GPL) 32-bit BASIC Compiler for Microsoft Windows, protected-mode DOS

In FreeBASIC, maths on ANY pointers (equivalent to C's void*) are treated as though the ANY pointer was a byte width. ANY pointers cannot be dereferenced, as in C. Also, casting between ANY and any other type's pointers will not generate any warnings.

dim as integer f = 257
dim as any ptr g = @f
dim as integer ptr i = g
assert(*i = 257)
assert( (g + 4) = (@f + 1) )

C and C++

In C and C++ pointers are variables that store addresses and can be null. tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. 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 unspecified type and cannot be dereferenced. The address can be directly manipulated by casting a pointer to and from an integral type of sufficient size (not defined in the language itself, but possibly in standard headers).

C++ fully supports C pointers and C typecasting. C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. 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 auto_ptr, a sort of smart pointer which can be used in some situations as a safe alternative to primitive C pointers. In C++, the Standard Library is a collection of classes and functions, which are written in the Core language. auto_ptr is a template class available in the C++ Standard Library (declared in) that provides some basic RAII features In computer science a smart pointer is an Abstract data type that simulates a Pointer while providing additional features such as automatic garbage collection C++ also supports another form of reference, quite different from a pointer, called simply a reference or reference type. In the C++ Programming language, a reference is a simple reference datatype that is less powerful but safer than the Pointer type inherited from

Pointer arithmetic, that is, the ability to modify a pointer's target address with arithmetic operations (as well as magnitude comparisons), is restricted by the language standard to remain within the bounds of a single array object (or just after it), though many non-segmented architectures will allow for more lenient arithmetic. Adding or subtracting from a pointer moves it by a multiple of the size of the datatype it points to. A data type in Programming languages is an attribute of a datum which tells the computer (and the programmer something about the kind of datum it is For example, adding 1 to a pointer to 4-byte integer values will increment the pointer by 4. This has the effect of incrementing the pointer to point at the next element in a contiguous array of integers -- which is often the intended result. Pointer arithmetic cannot be performed on void pointers because the void type has no size, and thus the pointed address can not be added to. The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that produces no direct result For working 'directly' with bytes they usually cast pointers to BYTE*, or unsigned char* if BYTE isn't defined in the standard library used.

Pointer arithmetic provides the programmer with a single way of dealing with different types: adding and subtracting the number of elements required instead of the actual offset in bytes. (though the char pointer, char being defined as always having a size of one byte, allows the element offset of pointer arithmetic to in practice be equal to a byte offset) In particular, the C definition explicitly declares that the syntax a[n], which is the n-th element of the array a, is equivalent to *(a+n), which is the content of the element pointed by a+n. This implies that n[a] is equivalent to a[n].

While powerful, pointer arithmetic can be a source of computer bugs. A software bug (or just “bug” is an error flaw mistake Failure, fault or “undocumented feature” in a Computer program that prevents it It tends to confuse novice programmers, forcing them into different contexts: an expression can be an ordinary arithmetic one or a pointer arithmetic one, and sometimes it is easy to mistake one for the other. A programmer is someone who writes Computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist In response to this, many modern high level computer languages (for example Java) do not permit direct access to memory using addresses. Also, the safe C dialect Cyclone addresses many of the issues with pointers. The Cyclone Programming language is intended to be a safe dialect of the C programming language. See C programming language for more criticism. tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured

The void pointer, or void*, is supported in ANSI C and C++ as a generic pointer type. A pointer to void can store an address to any data type, and, in C, is automatically cast to any other pointer type on assignment, but it must be explicitly cast if dereferenced inline. K&R C used char* for the “type-agnostic pointer” purpose. The C Programming Language (sometimes referred to as K&R or the white book) is a well-known Computer science Book

int x = 4;
void* q = &x;
int* p = q;  /* void* automatically cast to int*: valid C, but not C++ */
int i = *p;
int j = *((int*)q); /* when dereferencing inline, there is no automatic casting */

C++ does not allow the automatic casting of void* to other pointer types, not even in assignments. This was a design decision to avoid careless and even unintended casts, though most compilers only output warnings, not errors, when encountering other ill casts.

int x = 4;
void* q = &x;
// int* p = q; // This fails in C++: there is no autocast from void*
int* a = (int*)q; // C-style cast
int* b = static_cast<int*>(q); // C++ cast

In C++, there is no void& (reference to void) to complement void* (pointer to void), because references behave like aliases to the variables they point to, and there can never be a variable whose type is void.

C#

In the C# programming language, pointers are supported only under certain conditions: any block of code including pointers must be marked with the unsafe keyword. C# (pronounced C Sharp is a Multi-paradigm Such blocks usually require higher security permissions than pointerless code to be allowed to run. The syntax is essentially the same as in C++, and the address pointed can be either managed or unmanaged memory. However, pointers to managed memory (any pointer to a managed object) must be declared using the fixed keyword, which prevents the garbage collector from moving the pointed object as part of memory management while the pointer is in scope, thus keeping the pointer address valid. In Computer science, garbage collection ( GC) is a form of automatic Memory management.

The .NET framework includes many classes and methods in the System and System. Runtime. InteropServices namespaces (such as the Marshal class) which convert . NET types (for example, System. String) to and from many unmanaged types and pointers (for example, LPWSTR or void *) to allow communication with unmanaged code.

D

The D programming language is a derivative of C and C++ which fully supports C pointers and C typecasting. The D programming language, also known simply as D, is an object-oriented, imperative, multiparadigm System programming language However 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

Fortran-90 introduced a strongly-typed pointer capability. Fortran (previously FORTRAN) is a general-purpose, procedural, imperative Programming language that is especially suited to Fortran pointers contain more than just a simple memory address. They also encapsulate the lower and upper bounds of array dimensions, strides (for example, to support arbitrary array sections), and other metadata. An association operator, => is used to associate a POINTER to a variable which has a TARGET attribute. The Fortran-90 ALLOCATE statement may also be used to associate a pointer to a block of memory. For example, the following code might be used to define and create a linked list structure:

type real_list_t
  real :: sample_data(100)
  type (real_list_t), pointer :: next => null ()
end type
 
type (real_list_t), target :: my_real_list
type (real_list_t), pointer :: real_list_temp
 
real_list_temp => my_real_list
do
  read (1,iostat=ioerr) real_list_temp%sample_data
  if (ioerr /= 0) exit
  allocate (real_list_temp%next)
  real_list_temp => real_list_temp%next
end do

Fortran-2003 adds support for procedure pointers. Also, as part of the C Interoperability feature, Fortran-2003 supports intrinsic functions for converting C-style pointers into Fortran pointers and back.

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. Modula-2 is a computer Programming language invented by Niklaus Wirth at ETH, around 1978 as a successor to his intermediate language Modula Some of the variants of Modula 2 (such as Modula-3) include garbage collection. Modula-3 is a Programming language conceived as a successor to an upgraded version of Modula-2.

Oberon

Much as with Modula-2, pointers are available. Modula-2 is a computer Programming language invented by Niklaus Wirth at ETH, around 1978 as a successor to his intermediate language Modula 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. Modula-3 is a Programming language conceived as a successor to an upgraded version of Modula-2.

Pascal

Pascal implements pointers in a straightforward, limited, and relatively safe way. Pascal is an influential imperative and procedural Programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small It helps catch mistakes made 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. A data type in Programming languages is an attribute of a datum which tells the computer (and the programmer something about the kind of datum it is 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. A data type in Programming languages is an attribute of a datum which tells the computer (and the programmer something about the kind of datum it is Trying to dereference a null pointer, named nil in Pascal, or a pointer referencing unallocated memory, raises an exception in protected mode. Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of a condition that changes the normal flow of execution In computing protected mode, also called protected virtual address mode, is an operational mode of X86 -compatible Central processing units (CPU Parameters may be passed using pointers (as var parameters) but are automatically handled by the static compilation system. In Computer programming, a parameter is a variable which takes on the meaning of a corresponding Argument (computer science is same article--> argument

See also

External links

In Computer security and programming, a buffer overflow, or buffer overrun, is an anomalous condition where a process attempts to In a multithreaded computing environment a hazard pointer is an element used by a Methodology that allows the memory allocated to the nodes In Computer programming, an opaque pointer is a Datatype that hides its internal Implementation using a Pointer. In Computer science, pointer swizzling is the conversion of references based on name or position to direct Pointer references In Computer science, a reference is an object containing information which refers to data stored elsewhere as opposed to containing the data itself Static code analysis is the analysis of computer Software that is performed without actually executing programs built from that software (analysis performed on executing In Computer science a bounded pointer is a Pointer that is augmented with additional information that enable the storage bounds within which it may point to be deduced
© 2009 citizendia.org; parts available under the terms of GNU Free Documentation License, from http://en.wikipedia.org
Dapyx Software network: MP3 Explorer | Ebook Manager | Zenithic