Logo

dev-resources.site

for different kinds of informations.

The unspoken question: "Why do pointers exist?"

Published at
6/11/2024
Categories
clang
cpp
c
gcc
Author
ebbewertz
Categories
4 categories in total
clang
open
cpp
open
c
open
gcc
open
Author
9 person written this
ebbewertz
open
The unspoken question: "Why do pointers exist?"

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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

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

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);
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

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?

clang Article's
27 articles in total
Favicon
Tester c'est tricher, compiler c'est douter
Favicon
Pointers in Modern C
Favicon
Creating a Robust Logging System in C
Favicon
The unspoken question: "Why do pointers exist?"
Favicon
Pointers : what are they pointing to?
Favicon
Setting up linters in Gitlab CI for C++ and Groovy / Jenkins code
Favicon
SORRY, RUST & DEVS !
Favicon
Cycle don't want to write to the array
Favicon
cách xử lý lỗi không chạy được code C++ trong Code::Blocks "Tried to run compiler enumerable `C:\MinGW/bin/gcc.exe` but failed"
Favicon
Building an XDP eBPF Program with C and Golang: A Step-by-Step Guide
Favicon
Should I Learn Math First, Then Rust or C, or Can I Learn Mathematics and Rust, C Simultaneously?
Favicon
The C Programming Language by Brian W. Kernighan & Dennis M. Ritchie.
Favicon
The C Programming Language by Brian W. Kernighan & Dennis M. Ritchie.
Favicon
Dynamic Linker Hijacking Experiments - Evasive Techniques (Part 1)
Favicon
How To Include ‘bits/stdc++.h’ Header File With Clang Compiler on macOS
Favicon
How many asterisks can be put?
Favicon
A common pitfall when using sizeof() with pointers
Favicon
Platform detection in C&C++
Favicon
Lint Lint Boom
Favicon
Behind C++ Lambda Functions
Favicon
C++ Levitation: Looking for contributors
Favicon
Type qualifier: register, volatile and restrict - C Programming
Favicon
Configuring Oni as a C / C++ IDE on Ubuntu 18.04
Favicon
Use `bool` in C program
Favicon
Clang vs GCC
Favicon
Working on Object Lifetime Analysis for C++
Favicon
Writing safer C with Clang address sanitizer

Featured ones: