Set shared_ptr with new_pointer that is old_pointer + offset

Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr // make sure you use an array deleter std::shared_ptr<char> osp(new char[1024], std::default_delete<char[]>()); // load the data into your buffer at osp.get() // Find the offset in the data by parsing auto const offset = parse_buffer_for_offset(osp.get()); // Now set a … Read more

Can ptrdiff_t represent all subtractions of pointers to elements of the same array object?

Is it even possible for the result of i-j not to be in the range of representable values of ptrdiff_t? Yes, but it’s unlikely. In fact, [support.types.layout]/2 does not say much except the proper rules about pointers subtraction and ptrdiff_t are defined in [expr.add]. So let us see this section. [expr.add]/5 When two pointers to … Read more

How to avoid pointer arithmetic when using char** argv

From clang-tidy – cppcoreguidelines-pro-bounds-pointer-arithmetic: Pointers should only refer to single objects, and pointer arithmetic is fragile and easy to get wrong. span<T> is a bounds-checked, safe type for accessing arrays of data. So yes: Is there an alternative way to use the values of argv without using pointer arithmetic? Isn’t accessing a char** by any … Read more

Pointer arithmetic in Go

No. From the Go FAQ: Why is there no pointer arithmetic? Safety. Without pointer arithmetic it’s possible to create a language that can never derive an illegal address that succeeds incorrectly. Compiler and hardware technology have advanced to the point where a loop using array indices can be as efficient as a loop using pointer … Read more

Pointer arithmetics with two different buffers

To add the standard quote: expr.add#5 When two pointer expressions P and Q are subtracted, the type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined as std::ptrdiff_­t in the <cstddef> header ([support.types]). (5.1) If P and Q both evaluate to null pointer values, the … Read more

Does the C standard permit assigning an arbitrary value to a pointer and incrementing it?

The assignment: void *ptr = (char *)0x01; Is implementation defined behavior because it is converting an integer to a pointer. This is detailed in section 6.3.2.3 of the C standard regarding Pointers: 5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, … Read more

Pointer Arithmetic [closed]

Here is where I learned pointers: http://www.cplusplus.com/doc/tutorial/pointers.html Once you understand pointers, pointer arithmetic is easy. The only difference between it and regular arithmetic is that the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. For example, if you have a pointer … Read more