What are pointers?

10

I've come across this in several languages, especially C and C ++, but I've never understood what it is, how it's used, and why it exists. I discovered unintentionally that it also exists in C # and is an unsecured practice.

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

I've also read that pointers are references for variables, that this can be done with out and ref in C # (this does not exist in C ++ as far as I know). What would be the difference with these pointers?

public unsafe void Foo(char* teste) {
    *teste = 'A';
}
public void Bar(ref char teste) {
    teste = 'A';
}
    
asked by anonymous 03.01.2018 / 21:47

2 answers

12

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

Pointers are Indents . They can be called pointers. As they point to something, they are always an address of something. Although it can be used in other contexts, in general programming, these addresses are from 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 such city.

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

Then it is an integer , generally positive, that indicates the memory position of an object any.

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

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 location; at the highest level this position may even be indicated in the code with a variable variable name .

Indentation is important to give you flexibility and allow for a series of tricks that make it easy to create structures and algorithms for a requirement . If you did not have the pointer everything you need would have to be used in a linear fashion, you would have to copy the data you need by duplicating them , would complicate the management of memory, 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 . The dynamic allocation only works because of it .

Think of the index of a book, it has pointers to the pages where that information is in the book. Or at a job you do and say on what page the book was got that. Imagine without this information how difficult it is to find what you want, you would have to search the whole book.

Pointer is an abstract concept that has some processor support to deal with. Programming languages have ways to implement access to pointers in a slightly easier way for a human. C-like languages improve a bit , but not much because you still have to tinker with the pointer.

Other languages prefer, or only allow, to use a 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 access to arbitrary memory address without control. out , ref and the new in are references, that is, they are pointers to some object in memory, but it is not possible to manipulate the pointer any 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.

    
03.01.2018 / 22:25
2

In addition, references in C ++ 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 a reference, that means that it can have its values modified, but without the freedom of the raw pointer. In particular, a reference can not be redefined, ie, point to another address.

    
05.01.2018 / 18:51