What are the pointers?

Asked

Viewed 1,470 times

12

I’ve come across this in several languages, mainly C and C++, but I’ve never understood what it is, how it’s used, and why it exists. I found out unintentionally that it also exists in C# and it’s a practice unsafe.

What are the pointers for? How to use them? When they are useful?

I also read that pointers are references for variables, that it is possible to do with out and ref in C# (this does not exist in C++ as far as I know). What would be the difference of this with such pointers?

public unsafe void Foo(char* teste) {
    *teste = 'A';
}
public void Bar(ref char teste) {
    teste = 'A';
}

2 answers

13


Most of the specific questions here have already been answered in other questions.

Pointers are indirect. They can be called pointers. As they point to something, they are always an address of something, somewhere. Although it can be used in other contexts, in programming in general, these addresses are in memory and point to objects that are in this position. So we can say that the pointer is the position (coordinate) where your home is. latitude and longitude, street and number in the city such.

A pointer usually has 4 or 8 bytes and on older, or currently very limited devices, may have 2 or even 1 byte, except for exoteric architectures. I doubt that one day it will need more than 8 bytes that already allows you to address more than all the information currently produced in the world on a single device, that is, it can address 2 to 64 positions. No current architecture even supports near that.

So he’s an integer, generally positive, indicating the memory position of any object.

It can be stored in a processor logger, at some level of cache, RAM or even elsewhere, although if it is used in a different or persistent process, it is almost certain that something will not work at another time of execution.

Just like any stored number, you can assign a name in the code to it, that is, it can stay in a variable. Technically a pointer points to a memory position, at the highest level that position may even be indicated in the code with a name of variable.

Indirect is important to give flexibility and enable a number of tricks that facilitate the creation of structures and algorithms suitable for a requirement. If you didn’t have the pointer everything you need would have to be used in a linear way, it would have to copy the data you need by duplicating it, would complicate memory management, access would be extremely slow (almost everything that has complexity O(1) today would have to be O(n), is a brutal difference), among other problems.

A array is feasible because of the pointer. To dynamic allocation only works because of it.

Think of a book’s index, it has pointers to the pages where that information is in the book. Or in a work that you do and say on what page of the book that was obtained. Imagine without that information how complicated it is to find what you want, you’d have to search the whole book.

Pointer is an abstract concept that has some processor support to handle it. Programming languages have ways to implement pointer access a little easier for a human. Languages like C improve a little, but not much because you still have to mess with the pointer.

Other languages prefer, or only allow, to use one reference, which in the background is a pointer more controlled.

The C# pointer is a middle ground between pointer and reference. It is insecure, but not as much as in C because it does not allow accessing arbitrary memory address without control. out, ref and the new in are references, ie are pointers to some object in memory, but it is not possible to manipulate the pointer the way you want, the compiler does all the work and control for you. Objects created with classes and delegates use references, but internally they are pointers.

  • Just by complementing, in C++ you can use references when you define in the signature of a function that the argument will be passed with & for example void func(int &var). In this case the variable will be passed by reference and will be modified within the function, but without all freedom of "raw" pointers (raw)

  • To complement: Pointers are platform dependent, in c# there is an object that represents the pointer of the specific platform: Intptr. Also the fact that it is unsafe refers to the constant fact that it is easy to err with pointers and cause the famous "Segmentation fault", with strongly typed vector if you extrapolate its limits the framework does the checking and generates an exception and you can treat itThere usually with pointers there is no kind of conference... Despite this carteristic pointers are quite useful if you need extreme optimization, you earn some machine instructions.

2

To complement, in C++ references are defined using the operator &.

So we can have:

int number = 5;
int &myRef = number;

And we can have:

void func(int &var){...}

This second case is perhaps more interesting because it defines that the variable passed to the function goes as reference, it means that it can have its values modified, but without all the freedom of the "raw" pointers (raw Pointer). In particular, a reference cannot be redefined, meaning pointing to another address.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.