xmh0511
xmh0511

Reputation: 7369

some confusions about pointer to an object

Question 1:what is pointer value?

A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory ([intro.memory]) occupied by the object54 or the first byte in memory after the end of the storage occupied by the object, respectively.

Does it mean that pointer value is the address of an object?

Question 2:How to understand that **the pointer value is unchanged **?

[expr.static.cast]/13

Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b. Otherwise, the pointer value is unchanged by the conversion.

Consider the below example:

#include <iostream>
int main(){
   int a = 0;
   void* tmp = &a;
   char* obj = static_cast<char*>(tmp);
}

According to the above quote,we know that the original pointer values of tmp is pointed to a,and the destination pointer values point to an object of type char,since they are not pointer-interconvertible,hence the sentence the pointer value is unchanged by the conversion worked.Does it mean that obj is now a pointer point to an object of type char and dynamic type of the object is int,if I misunderstand about pointer value is unchanged,what does this sentence mean?

Question 3: How to understand that the result is a pointer to b.

#include <iostream>
struct Data{
  int c;
}
int main(){
  Data d;
  void* tmp2 = &d;
  int* ptr = static_cast<int*>(tmp2);
}

we know that the original pointer values of tmp2 is pointed to d,so the original pointer value point to an object of type Data,and the destination pointer point to an object of type int,and these two objects are pointer-interconvertible,So the sentence the result is a pointer to b worked.Does it mean that now ptr pointer to an object of type int and dynamic type of the object is also int?

Upvotes: 2

Views: 120

Answers (2)

eerorika
eerorika

Reputation: 238401

Does it mean that pointer value is the address of an object?

Colloquially, yes that would be quite true. More precisely though, it means that pointer value is an address. The value of an object pointer could be the address of an object, but it can also be an invalid pointer value, null, or indeterminate.

Does it mean that obj is now a pointer point to an object of type char

Yes.

if I misunderstand about pointer value is unchanged,what does this sentence mean?

Simply, the address is the same. obj is a pointer to char and it points to the same address where the int object is stored. But obj is not a pointer to a.


As far as I understand, the distinction between having a pointer to object b, versus having a pointer whose value is the same as the address of b is that you can always indirect through the pointer to b to access the pointed object legally.

Simply having a pointer to type T with same value (i.e. the pointed address) as another object does not by itself mean that it is well defined to access through the pointer. Even if there happens to exist an object of that type at the address (in which case you can get pointer to the object by std::laundering the pointer). In your example though, it is legal to indirect through obj, because pointers to narrow character types are special.

In short, a pointer always points to an address. It may or might not point to an object at that address. There might be multiple objects with the same address (only in cases that involve sub objects). Not all addresses contain an object at all times.

Upvotes: 0

Useless
Useless

Reputation: 67772

A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory ([intro.memory]) occupied by the object or the first byte in memory after the end of the storage occupied by the object, respectively.

Does it mean that pointer value is the address of an object?

No.

This sentence structure "A or B means X or Y, respectively," is read as "condition A implies X, or alternatively condition B implies Y."

So the sentence means both

A value of a pointer type that is a pointer to an object represents the address of the first byte in memory ([intro.memory]) occupied by the object ...

and

A value of a pointer type that is a pointer past the end of an object represents the address of the first byte in memory after the end of the storage occupied by the object.

The respectively allowed the authors to de-duplicate the preamble I had to repeat when expanding it out like this.

And, after all that, your meaning wasn't quite correct even for the first case.

It means that a pointer, if it is a pointer to an object, represents the address of the object's first byte in memory. It doesn't say anything about the value of any pointer unless it is a pointer to ... an object.

It doesn't mean that every pointer value is the address of an object, because pointers can be uninitialized, or set to nullptr, or set to the result of a raw allocation which has not yet been turned into an object with placement new. A pointer can still hold the address of the object you just delete-d via the pointer, as well.

I know the standard isn't super easy to read, especially if you're not used to this style (or to English) - but that means you need to make some effort to read it carefully.

Upvotes: 1

Related Questions