We can say that conceptually pointers are abstract.
In fact almost everything is abstract, concrete even only excited electrons (people rave when I speak like this :D) circulating by static electrons in computer materials. The bit is already an abstract concept.
Things are more abstract or less abstract, but some level of abstraction always has, at least in things created by humans. We must always consider levels of abstraction and not whether or not it is abstract. Even a word that indicates something is already an abstraction.
Pointer is a concept that a human created to imply that we have a indirect. The most common is to be in memory, but do not need to be, we have in other contexts, for example one can have a pointer in database (that if people knew how to do could make some savings).
In memory, the most commonly used concept in programming, in fact a pointer is represented by a memory address.
From an abstract point of view (i.e., from the moment memory is abstracted) it does not matter whether physical memory is linear or not. Abstraction serves precisely for this.
Measuring the level of abstraction is not always easy, it becomes subjective. What is the minimum and maximum abstraction? Goes from 0 to 10, or from 0 to 100? Or it’s not even how you measure it. We don’t have parameters for it.
We can say that he is more concrete than the reference.
And the mechanism is a more abstract way of accessing memory.
Abstractly you shouldn’t worry as much as they are implemented, but the lower the level goes the more important it is to know this.
I don’t know if you have this dichotomy that question leaves. Or even if it has relevance. But I will try to answer.
But it exists even in Assembly, it is a type that deals with something that the processor understands, as well as the numerical types. Can you understand the level of concreteness? It is more concrete than string, a date, or even a array which is more abstract than a pointer, because it depends on the pointer to exist.
In the exposed form it seems to speak of the kind that is a inter, not only of the mechanism. And then the doubt whether it would be a concrete type or a abstract type makes sense.
My understanding is that it is more concrete, it is a concept that exists in the computer, it is not the same as a int
or long
. He is not another type composed solely of a whole, he has a form of his own. This is the opposite of what happens to a date for example that has the amount of seconds or fractions and seconds represented as integers or other "concrete" numerical type, or the interaction between numerical data representing years, months, days and who knows hours, and other fractions of days.
A int
can be interpreted as a pointer? It can, but this is circumstantial, not always this is true.
"Pointer type"
Another question was asked which is completely different from what was asked in the title, perhaps for not understanding this.
Differentiating into what the pointer is pointing to has nothing to do with the pointer itself. Telling what it points to is important to know the size it is pointing to and how far it should access that information, or even calculate the virtual position of the memory of a sequential structure (array).
The compiler needs this information to make choices and generate proper code, and in some cases force typing (not so much because C has weak typing, but some implementations have options to strengthen typing and become even more important).
The issue of malloc()
it’s just a case I just talked about, but it’s orthogonal to the pointer or its type, it does not need to know the size of the pointer or its pointed type.
The malloc()
allocate bytes, ever, the sizeof
can be used to catch the size of something. The sizeof
can be used to pick up the size of the pointer because depending on the architecture its size is different. But it makes no sense to take the size of the pointer according to the type, because the size of the pointer does not change, it changes the size of the pointed object.
sizeof char
(people always use the parentheses because they think it’s a function, but it’s an operator) it doesn’t make sense because the language specification says it’s guaranteed to be 1. Who does it is usually terraplanist, thinks that one day can change :D.
Accepting something doesn’t mean anything, C accepts almost everything. Allocating 1 byte (char
) is as accepted as allocating 4 bytes typically (int
, may be another size), but allocates quite different. This has nothing to do with pointer.
The complete dice type when using pointer is "Pointer of something", it’s not just pointer, never, and it’s not just the type. You can make a pointer for nothing (void
), which in practice means that it is for anything, you give up the size of the pointed object.
In C the pointers tend to be relatively safe, if you use it right, in Assembly it’s pure mathematics, but if they were generated by a C compiler as they work perfectly.
Just remembering that the specification of C, not only in this, is very open to implementation do as you wish, but in practice, that’s what I explained here.
Has interesting additions in the comments above and below.
The reason to differentiate a pointer to
char
from one toint
is only for type checking during compilation. Both pointers have the same representation in memory, but it is not good to know what kind of data the pointer is pointing to when reading/writing the code?– Andre
For the purpose of type checking in the compilation I think it is useful information yes. But in theory I can circumvent this and declare for example
int *str = malloc(sizeof(char));
(or vice versa) that the compiler accepts, or not? I will take the test.– Piovezan
I do not know the practical consequences of this in the programme, but it accepts both: https://ideone.com/qUsO1V
– Piovezan
The type makes a difference when doing pointer arithmetic, basically
&x + 1
picks up the address ofx
and sumsizeof x
bytes (not 1). It also makes a difference by taking the value it points to. Ex: https://ideone.com/VoghTL– hkotsubo
@hkotsubo Very well noted, I didn’t know/remember that there was a difference in pointer mathematics, and also when retrieving content in memory, two great reasons to include the type in the statement :)
– Piovezan
But there is still a doubt: internally the type of data pointed is part of the pointer? Or this is all, as was said, for resolution purposes in compilation and then is converted into "dangerous" pointers let’s say so, I mean, in the sense of positions/memory structures that have no type of content associated?
– Piovezan
Then I don’t know. I think it makes sense for the pointer to have the type information it points to, otherwise it would be difficult to perform operations like arithmetic and dereferencing (there is that word?). But I don’t know if the language specification says anything about it (and I’m too lazy to look up :-p)
– hkotsubo
Supplementary reading: https://stackoverflow.com/q/15151377 | https://stackoverflow.com/q/1352500 | https://stackoverflow.com/q/950972 | https://stackoverflow.com/q/44345148
– hkotsubo