Print Email Download Reference This Send to Kindle Reddit This
submit to reddit

The Fundamentals Of Data Structures Computer Science Essay

A pointer references a location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing the pointer. A pointer is a simple, less abstracted implementation of the more abstracted reference data type. Several languages support some type of pointer, although some have more restrictions on their use than others.

Pointers to data significantly improve performance for repetitive operations such as traversing strings, lookup tables, control tables and tree structures. In particular, it is often much cheaper in time and space to copy and de-references pointers than it is to copy and access the data to which the pointers point.

Pointers are also used to hold the addresses of entry points for called subroutines in procedural programming and for run-time linking to dynamic link libraries. In Object-oriented programming, pointers to functions are used for binding methods, often using what are called virtual method tables.

A memory pointer is a primitive, the value of which is intended to be used as a memory address; it is said that a pointer points to a memory address. It is also said that a pointer points to a datum [in memory] when the pointer's value is the datum's memory address.

More generally, a pointer is a kind of reference, and it is said that a pointer references a datum stored somewhere in memory; to obtain that datum is to dereference the pointer. The feature that separates pointers from other kinds of reference is that a pointer's value is meant to be interpreted as a memory address, which is a rather 'low-level' concept.

References serve as a level of indirection: A pointer's value determines which memory address is to be used in a calculation. Because indirection is a fundamental aspect of algorithms, pointers are often expressed as a fundamental data type in programming languages; in statically typed programming languages, the type of a pointer determines the type of the datum to which the pointer points.

Syntax of Pointer Declaration

The * character is used to refer to a pointer.

int variable /* declares an int variable */

int * variable /* declares a pointer to an int variable */

int * variable[5] /* declares an array of 5 pointers, each to an int variable */

Pointer syntax, especially if arrays are involved can get horribly complex. My advice is to use typedef to simplify the declarations.

EXPAMLE OF PIONTERS

The variable “Fred” is a string, or more accurately a pointer to an array of character.

Arrays and pointers are both treated as addresses.

When the compiler builds in a string like this, it adds a hidden '\0' so it will allocate space for 5 characters 't','e','x','t',\0.

Fred is just that, a variable and so can be changed.

The variable okfred is also a char * but points to nothing as it has not been explicitly initialized.

While a is just a normal int variable, d is a pointer to an int variable, initialized to point to a and so *d is the same as a. Later on we increment the int variable that d is pointing at (i.e. a) so a and *d, both have the same value 47. They ARE the same variable!

Fred2, another char * is set equal to fred. That is, it contains the same address that fred has and so when fred2 is output, you see "text". Fred takes on another value later but Fred2 still points to the text that Fred initially pointed to.

Ptrb, like d is an int pointer that initially points to d. The value contained in *ptr (at this point it is still 46) is copied into c.

Now notice the difference between *++ptra and ++(*d)

The first one increments the pointer ptra so it points to the next int after a (if there is one). ++ (*d) however increments what d points to, which is a so*d and a both have the value 47. You can see how confusing pointer expressions can get! Pointers let you write some really unreadable code. In the next lesson, we'll look at how to understand complex pointer declarations.

Advantages & Disadvantages of Pointers

I want to do this! What's This?

Pointers provide a mechanism for directly accessing and modifying objects and code in memory. Pointers are used in many programming languages for manipulating character strings, passing parameters to functions, handling arrays of data, and creating linked lists and other complex data structures. While pointers provide you with a great deal of flexibility for managing memory and optimizing algorithms, there are disadvantages. Learning to use pointers is difficult for many programmers, and debugging pointer problems is challenging.

Optimization

Pointers provide a performance advantage by allowing you to access computer memory directly. In a computer program, the fastest way to access and modify an object is to directly access the physical memory where that object is stored. This technique is commonly used to optimize algorithms requiring frequent or repetitive access to large amounts of data.

Security

Direct access to memory means you can do things that perhaps you should not. Depending on the language, the compiler and the computer platform, you could unintentionally access memory that is not yours to access. As a result, you could overwrite critical memory, modify the code of a running application, or cause your application or another application to behave or exit unexpectedly. One common error is to run past the end of an array, accessing the memory beyond the end of the space allocated for that array.

Memory Management

Managing memory usage in an application using pointers offers flexibility but is also a chore. Allocating and de-allocating memory as needed during run time allows you to create large objects, such as arrays, quickly and immediately free the memory when it is no longer required. However, it is also easy to create memory leaks by reassigning a pointer without freeing the memory it was pointing to first. Garbage collection is your responsibility.

Function Parameters

Functions can return only one value, but they can take multiple parameters. By passing in pointers to variables as parameters, a function can be used to set the values of those variables, and the new values will persist after the function returns. Being able to set the value of several variables at once with a single function call is clean and efficient. However, it can be a little confusing to read because you can't tell if the variables passed in will be modified or not simply by looking at the function call. You will need to look at the function documentation to learn how the function behaves.

Function Pointer

Pointers are not just for objects in memory; they can also be used for functions, thus allowing a function to be passed as a parameter to another function. A function pointer can be assigned the address of one of a choice of functions so that the pointer acts as a kind of alias. Object-oriented programming languages have eliminated the need for function pointers with inheritance and polymorphism.

Pointer Confusion

Pointers are a confusing topic for many programmers, particularly those experienced in using languages that do not feature pointers and direct memory access. However, persistence leads quickly to mastery of pointers. Pointers are often accompanied by obscure syntax. Consistent variable naming conventions can help your code be more readable. For example, common naming conventions include using "p_" or "ptr_" as a prefix to pointer variable names.

USES OF PIONTERS

Pointers are directly supported without restrictions in languages such as PL/I, C, C++, Pascal, and most assembly languages.

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 functional programming languages that rely heavily on lists, pointers and references are managed abstractly by the language using internal constructs like cons.

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. If the data elements in the array have lengths that are divisible by powers of two, this arithmetic is usually much more efficient.

Pointers are used to pass parameters by reference. This is useful if the programmer wants 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.

Pointers can also be used to allocate and de-allocate dynamic variables and arrays in memory. Since a variable will often become redundant after it has served its purpose, it is a waste of memory to keep it, and therefore it is good practice to de-allocate it when it is no longer needed. Failure to do so may result in a memory leak.

Some Rules that should be taken in mind while using with Pointers

These are more common sense than hard and fast rules.

Always initialize pointer variables.

With strings, never forget to include the terminating '\0' when calculating the storage size.

Check the return value of malloc() and make sure it's not NULL before using it then cast it to the desired type.

For every malloc() there is an equal and opposite free()!

Remember that &variable is a pointer.

After freeing a pointer always set it to NULL.

For Example

int * pint=(int *)malloc(sizeof(int)) ;

/*Do something with pint*/

free(pint) ;

pint = NULL;

Setting pint to NULL is a safety feature. Just because the memory has been released by the call to free() does not prevent it accidentally being reused. This could lead to a nasty bug. Forcing it to NULL will show any "freed pointer reuse" bugs much sooner.

Print Email Download Reference This Send to Kindle Reddit This

Share This Essay

To share this essay on Reddit, Facebook, Twitter, or Google+ just click on the buttons below:

Request Removal

If you are the original writer of this essay and no longer wish to have the essay published on the UK Essays website then please click on the link below to request removal:

Request the removal of this essay.


More from UK Essays