How to use pointers effectively, and when to use them

Author Name
Answered by: Melissa, An Expert in the C Category
Learning how to use pointers is the single most powerful thing any programmer can learn to use; however, because of that they are also the hardest tool to learn and frequently the least well understood.

With the rise of languages with built-in garbage collection in colleges and the workplace (Java being the most visible) many programmers have seemingly decided that not having to do their own memory management means that pointers are just a good way to shoot themselves in the foot, and thus should be ignored. This is entirely false, as even if you don't actively need to use them, knowledge of how pointers work will help you gain greater understanding of many things being done in the background by your libraries or garbage collectors. This is particularly important when it comes to debugging complex, dynamic systems.

As a quick refresher on pointers and what they are, a pointer is a variable that stores the address of another piece of memory. So if you have a basic integer variable Foo at address 1000, and you create an int pointer Bar at address 2000 “pointing” at Foo, then Bar will contain the value 1000. This allows you to point to arbitrary memory addresses (with certain exceptions), which is most useful when dynamically allocating memory, which compose the majority of pointer usage cases. However, being able to maintain a pointer to a singular memory address as it changes over time, or a single object as it changes memory positions, can be very useful niche applications of pointers as well.

So, how is knowing how to use pointers going to benefit you? As mentioned, they are incredibly important for memory management, and the singular source of memory leaks. What a memory leak is, precisely, is when a pointer to a chunk of allocated memory is lost before the memory is deallocated. The process that performed the allocation now has no ability to reference the memory and the memory will just stay uselessly allocated, eating system resources, until the process is concluded.

Pointers can also lead to some of the most obscure issues in programming, such as a pointer of a user created object type maintaining the address of an object that you believed to have been deleted earlier, leading to you spending hours trying to figure out why your program is continually giving you old, incorrect data.

Those are just the most basic of the extremely perplexing bugs pointers can cause, but for someone with even a moderate level of experience in actively using pointers, these problems and their solutions will reveal themselves very quickly.

If there is anything you should take away from this, it is that even being cognizant of pointers, and how they are used, will give you a much greater intuitive understanding of how many things you make use of everyday in your programming (such as data structure libraries) work. This will allow you to use them in your systems far more efficiently and effectively, making your code that much better.

Author Name Like My Writing? Hire Me to Write For You!

Related Questions