The Limitations of  Python: An Introspective dig into Pointers.

Photo by Smart on Unsplash

The Limitations of Python: An Introspective dig into Pointers.

This article covers :

  • What pointers are

  • Types of pointers

  • Why do we use pointers

  • Use of Pointers in Embedded Systems

  • Dangers associated with the use of pointers

The greatest revolution currently ravaging the world is the advent of the Meta-Verse. Well, I was not shocked when Mark Zuckerberg began his quest to breathe life into people's wishes. I was not surprised because I also had this dream growing up as a young boy in a society where the television was the greatest of all times invention. I looked forward to the TV broadcast of Teletubbies every Saturday morning whenever my dad and my elderly siblings find their way to the farms. As a young boy, I was obsessed with the quality of toys on display in those shows, the thoughts of being able to grab hold of those toys and join the kids in playing by simply entering inside the TV screen overwhelmed me.

tub.jpg Daily, I kept wondering how I can save those humans imprisoned inside our TV box. I desired to save them perhaps I could be gifted toys by them.

One of the programming tools that is used to bridge the gap between reality and dreams is Pointers.

Why do I say this? Kindly remember that the computer has a memory as one of its components, this memory has addresses which are labelled. When we talk of a computer broadly, it does not exclude your ATM, Petrol Dispenser, Calculator, Mobile Phones etc. All these devices can perform their functions dutifully well because they have a memory and this memory can be utilized to perform one's desires. At the heart of this magic lies pointers.

mem.jpg

What is a Pointer

A pointer is a variable which is used to store the address of other variables. If you're a little bit confused about the statement "address of other variables", you can read my short article about variables and datatypes here Variables and datatypes. Whenever you declare a variable, what you're doing is allocating a portion of your computer memory to store data which can fit into the memory allocated. That particular portion has an address and this address is machine-dependent. Suppose you want to store this particular "machine-dependent" address, that's where pointers come in. They basically help you to store the address of your variables. Don't be tired, bad knowledge of pointers have a good number of formidable software!!!!, so the knowledge is worth it!!!!

desire.jpg

Types of Pointers

Basically, we have pointers to every kind of datatypes that exists. This means that, if I have an integer variable, the suitable pointer to store its address is an integer pointer. Pointers are often denoted with; the data type you're pointing to, an asterisk sign and the variable name of your pointer.

#include <stdio.h>
int main(void)
{
    int num = 6; 
    int *pNum = NULL;     // this is a pointer to an integer variable
    int pNum = &num;      // using pointer to store the address of the integer variable
    return 0;
}

There exists one type of pointer which deflects from the conventional behaviours of pointers.This pointer is known as the void pointer; which is written as void". This pointer can point to the memory address of any data type and it's the return type of dynamic allocation tools like malloc(),calloc() etc.; however, type casting such pointers is best as this will save us from unexpected behaviour in our programs. For example:

#include <stdio.h>
int main(void)
{

    int *pNum = NULL;     // this is a pointer to an integer variable
    int pNum = (int *)malloc(sizeof(int));      // allocating memory dynamically from heap memory
    //Always remember to deallocate dynamically allocated memory
    free(pNum);
    return 0;
}

Why do we use Pointers?

Before the advent of drones, fighter jets are flown by airforce pilots at the mercy of being struck by enemy missiles and the danger of the malfunctioning of such fighter jets. The advent of drones ushered in the era of control stations where pilots issue execute operations without having to endanger their lives. Such a feature is also possible in programming through what we call dereferencing. We use pointers to dereference memory addresses so that we can change the contents stored in those addresses. For instance,

#include <stdio.h>
int main(void)
{

    int num = 6; 
    int *pNum = NULL;     // this is a pointer to an integer variable
    int pNum = &num;      // using pointer to store the address of the integer variable
    *pNum = 12;               // this changes the value stored in num to 12
    printf("%d ", num);     //the value output is 12
    return 0;
}

This concept is largely used in Files, you can read my article on Files to gain more insights about File pointers Files pointers. Pointers are also very handy when it comes to dynamic memory allocation. They help to store values of memory addresses which are allocated from the heap section of the memory. Pointers are also very handy when they're used with arrays. They allay us the stress of inputting array subscripts and help us to carry out operations involving arrays faster.

Pointers in Embedded Systems

embed.jpg Imagine trying to turn on an LED using a Microcontroller, how do we communicate with that portion of the microcontroller which can help us in carrying out these tasks. Hardwares have what we call registers, these registers are programmable memory addresses dedicated to performing various operations in which hardwares are used, hence, gaining access to these memory addresses entails, grabbing hold of the memory and being able to access the memory to make our desired executions. These two operations are essentially performed by pointers and in this case, dereferencing is key.

Dangers Associated with the use of Pointers

The sole reason why a good number of the Gen-Z programming languages exempt the explicit use of pointers from them is the risk associated with its careless use. Personally, I've also misfired while using pointers, especially trying to dereference a memory that stores nothing, this makes the computer attempt plying a road that leads nowhere, I had to refrigerate my computer in Antarctica before it became balanced again. Some dangers associated with careless use of pointers are :

  • Segmentation faults: which are caused by uninitialized pointers. The best practice is to always initialize pointers immediately they're created.

    Uninitialized pointers abhor evil in our programs

  • Memory leakage: this occurs mostly when pointers are used to store the addresses of memory allocated dynamically and such memory addresses are not freed afterwards. This is highly disastrous to our program.

frus.jpg

Summarily, you've learnt the power of pointers today, especially how their use cut across everyday electronics. The more you use pointers, the more the reality that the bogus computer concepts that scare your understanding are products of advanced pointer implementations. Thanks so much for persevering thus far !!!! E Seun.