Home  >  Article  >  Backend Development  >  The unspoken question: \"Why do pointers exist?\"

The unspoken question: \"Why do pointers exist?\"

WBOY
WBOYOriginal
2024-07-16 16:20:01362browse

The unspoken question:

It's Really A Valid Question

This is not the typical pointer-hater's question, but rather a very interesting one.


Here's What I Mean

Pointers are a great powerfull concept, but that's what it is: a concept. So why was the C compiler invented with such an isolated piece of logic and syntax dedicated to pointers?

Don't get me wrong, I love the oppurtunities that pointers give us and their current syntax. They are an absolutely essential feature, having achieved the evolution of dynamic data structures, objects and classes, multithreaded memory sharing, object mutability, low-redundant value duplication and much more.
But if you imagine the event of the invention of C, the pointers as current-day seem a much more impressive idea then an intuitive first concept. Let's take a deeper look into what I mean.


A Deeper Look

If you look at a pointer's structure it is basically an unsigned long (aka 4[32bit system] or 8 bytes in memory). The things that separate pointers from unsigned longs are the pointer-specific features.

Syntax And Dereferencing Operator

A pointer has its own declaration syntax and has its propertary operator: the dereferencer.

int a = 5;
int *ptr = &a; //declaration
int value = *ptr; //dereference

But let's imagine, that this was never invented. Then the following would just be easily possible if the dereferencing feature was just associated with any integer type:

int a = 5;
unsigned long adress = &a;
int value = *adress;

In that case, you could even do things like this:

int firstIntInMemory = *(0); //manually dereferences (4bytes at) adress 0`

Speaking of the parser, this is totally not a conflicting syntax since star as derefrencer is a unary operator while star as arithmetic multiplicator is always a binary operator.
This fictional dereferencing operator as I described above is really the raw essence the pointer concept. Comparing this to the current real implementation makes the head question so interesting to think about. There could have been so many outcomes.

Pointer Arithmetic

The only special thing pointer arithmetic does is taking type sizes into accound with calculations. When I have an array, and I want to get the second element, I just add 1 to the pointer. If it is an int pointer, this wil implicitely actually add a value of 4 to the adress (if sizeof(int) == 4 on your system):

int arr[5] = {1,2,3,4,5};
int second = *(arr + 1);

But let's be honest, the following is actually much more logical if you think intuitively about memory:

int arr[5] = {1,2,3,4,5};
int second = *(arr + sizeof(int));

And this would just be standard integer arithmetic. If you look at it this way, there is not really a reason to have invented pointer arithmetic at all.


That's Not All

Off course, the '*'-syntax makes intended usages much more clear. If you see it, you immeditately know that this variable is used for memory manipulation. Also every memory manupulation library function is designed for pointers.

But still, if it was never invented, and instead we had these dereferencable unsigned longs, people would have just come up with design- and naming conventions, like appending pointer variable identifiers with a '_p' suffix. And memory manipulation libraries would just have evolved around this.


Final Word

So really, if you think about it, C could just have survived the same way as it lives right now if pointers were never invented as a feature of the language. They would just be invented as a concept by programmers, working the same as how they currently exist.

I find this an interesting story to investigate deeper into.
Why did C invent pointer?
Was it just the reason we expect: consistency, clarity and safety against misuse of derefrencing?
Or is there a deeper reason and a much more complex logic then how I covered pointers in this post, which makes them actually significantly more efficient than doing the same with general purpose integers?

The above is the detailed content of The unspoken question: \"Why do pointers exist?\". For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn