DEV Community

Discussion on: Pointer in C/C++

 
pentacular profile image
pentacular

Can you write a function taking the pointers and decide whether the pointers belong to the same array without having any more info available about the arrays?

You cannot introspectively determine this.

Just like you cannot introspectively determine that a pointer has a well defined value, or that a pointer points at allocated memory.

As far as I understand, this is not possible, because the pointers they don't belong to a container.

It is not possible because C and C++ do not provide introspective mechanisms to determine this.

However, they do assert that you cannot legitimately derive a pointer to one array from a pointer to another array.

(And as all objects are effectively stored in arrays in C and C++, this is a universal property and applies likewise to the substructure of STL containers, and so on.)

Which means that the association exists, regardless of if it is encoded into the program in a detectable fashion or not.

Just as memory being allocated or not may not be encoded into the program in a detectable fashion.

(For example, consider a compiler which has been able to statically determine all memory allocation your program will perform and so has hard coded every allocation at compile-time.)

The fact that you can't check it doesn't mean that it isn't significant. :)