Can you do arithmetic on char * by pointing to another object

This is the usual way to read object bytes with the ability to three-dimensional copy

Object obj; auto p = reinterpret_cast<char*>(&obj); for(size_t i = 0; i < sizeof(obj); i++) consume(p[i]); 

The problem is not with strict anti-aliasing; char* resolves an alias to something. The problem is this excerpt from [expr.add]

When an expression that has an integral type is added or subtracted from the pointer, the result is the type of the operand of the pointer. If the expression P points to the element x[i] object of the array x with elements n , the expressions P + J and J + P (where J has the value J ) indicate the (possibly hypothetical) element x[i + j] if 0 ≀ i + j ≀ n ; otherwise, the behavior is undefined. Similarly, the expression P - J indicates a (possibly hypothetical) element x[i βˆ’ j] if 0 ≀ i βˆ’ j ≀ n ; otherwise, the behavior is undefined.

Where the hypothetical element refers to

A pointer preceding the last element of an array x elements n is considered equivalent to a pointer to a hypothetical element x[n] for this purpose

In other words, this is legal if arithmetic is on a pointer pointing to an array and the result is still within its range.

However, there is clearly no char[sizeof(Object)] , can we do arithmetic on this pointer?

Note that the legal solution for reading bytes of an object is a std::memcpy object. But if this is the only solution, he asks to ask, why allow char* smoothing if you can’t do anything about it?

+7
c ++ language-lawyer
source share
1 answer

Pointer arithmetic must be legal according to quotation marks. An instance of Object obj can be thought of as char[sizeof(Object)] . So this is an array of n elements (note that n is sizeof(Object) ). The standard allows you to perform pointer arithmetic within the boundaries of this array plus one hypothetical element outside of this array. This is due to a lesser or equal sign in

0 ≀ i + j ≀ n

expression.

Literally reinterpret_cast<char*> (&obj) + sizeof(Object) excellent, because it points to a hypothetical element a[j] , where j = sizeof(Object) and it is less than or equal to the size of the array, which is equal to sizeof(Object) .

So the answer is yes.

Otherwise, std::end for arrays will be UB.

+1
source share

All Articles