Advantages & Disadvantages of Pointers

by Deborah Lee Soltesz

Depending on who you ask, pointers are either a blessing or a curse. 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 (or intentionally) 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 deallocating 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 Pointers

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.

Tips

Always use the allocation functions provided by the language for allocation memory, and check to make sure the memory was allocated before using it. Be careful with memory management, and free up memory when it is no longer needed. If you reassign a pointer to a new memory address, make sure the old memory is freed or assigned to a different pointer. Make sure that pointers created in functions are assigned ownership to the parent scope or that the memory is deallocated before exiting the function.

About the Author

Deborah Lee Soltesz is a web developer who has been creating websites, promotional materials and information products since 1992. Soltesz has a Bachelor of Science in computer science and engineering. She worked in earth and space science research writing for scientific and general audiences for more than a decade.