1

I am not able to grasp the reasons why pointers need a "type specifier". To my eyes they are just pointers so their type might be just like "ptr" and in a 32-bit machines I would expect them to occupy four bytes each of memory, whatever they point to.

When I need to retrieve a particular variable (int or double etc...) then the compiler, according to the type of some variable, would know "how many" bytes it should retrieve to get the whole. I probably I miss some logic and would appreciate your opinions.

Example:

#include "pch.h"
#include <iostream>

int main()
{
    ptr *value; // Why is this ILLEGAL?
    int test = 15;
    value = &test;

    std::cout << *value;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paolo
  • 173
  • 2
  • 9
  • 5
    So what's `++value` in your model? The next `int`? The next byte? – StoryTeller - Unslander Monica Mar 14 '19 at 08:46
  • 1
    Wel for pointer aritmetic for example, if you want the next pointer to be valid, the addres must be incremented with sizeof(*mypointer). – hetepeperfan Mar 14 '19 at 08:46
  • Note that if this were allowed, the same type of pointer would be allowed to point to an int and a `vector`. – juanchopanza Mar 14 '19 at 08:49
  • Taking the addres with the & operator, doesn't imbue the pointer with the type of int. So the compiler cannot see what value points to. So with `std::cout << *value`, wouldnt compile either, because the type isn't known. – hetepeperfan Mar 14 '19 at 08:50
  • As you wrote yourself, the compiler needs to know the type of the thing that the pointer points to. Where would it get that information from if not from the type of the pointer? – molbdnilo Mar 14 '19 at 08:54
  • 1
    @StoryTeller: Thanks for reply. Well...Now i get your point. If value was a int *value than value++ would be 4 byte "forward", in case of double, would be 8 byte forward. Since i was not thinking about operations on pointers, i was not able to grasp the reason...now i guess i do. – Paolo Mar 14 '19 at 08:55
  • @hetepeperfan : That's a very good reason too. Thank you. – Paolo Mar 14 '19 at 08:57
  • consider `int x = 0; ptr p = &x; double y = *p;`. If pointers had no types you could use them to circumvent type safety – 463035818_is_not_an_ai Mar 14 '19 at 08:58
  • 1
    It is also a syntax thing. IIRC Rust allows the "declaration" of the pointer/reference to have no specific type, and the type gets inferred when it is assigned to something. But then it cannot be re-assigned to something of a different type. So the pointer/reference has a type, but you don't specify it at the point of declaration. – juanchopanza Mar 14 '19 at 08:59

2 Answers2

4

To be able to tell what you expect inside. It points to a typed part of memory.

Otherwise all operations on a pointer (not the address itself) would be just blindly following leads.

Koshinae
  • 2,240
  • 2
  • 30
  • 40
0

Neither the C nor the C++ standard state that the sizeof of different pointers needs to be the same.

This is in order for C in particular to maximise its universality. For example, an architecture might have an off-CPU floating point processor, so perhaps sizeof(double*) is different to a sizeof(char*).

Having type information in a pointer is also necessary for pointer arithmetic to work in both C and C++ (pointer arithmetic is essentially compile-time evaluable, so the languages cannot resort to run-time type information). The static_cast of C++ is similarly compile-time evaluable.

Hence the type does matter, so ptr *value; would only make sense if ptr was a type.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 3
    This kind of implies that it is OK for pointers to point to things of different type as long as the `sizeof` is the same. – juanchopanza Mar 14 '19 at 08:47
  • @chqrlie: In fairness the comment was more referring to a version of this answer where the 3rd paragraph was missing. – Bathsheba Mar 14 '19 at 10:51
  • Indeed, the `sizeof` in the OP's comment is that of the object pointed to, not the pointer itself... my confusion. – chqrlie Mar 14 '19 at 10:53