DEV Community

Discussion on: Pointer in C/C++

 
sandordargo profile image
Sandor Dargo

That's perfectly right, we are speaking about two different things. You say that getting from one array to another via pointer arithmetics is undefined behaviour. It is! In fact, that's pretty much I talked about at C++ On Sea earlier this week. Except that, I didn't speak about arrays, but STL containers and not about pointer arithmetics, but iterators.

What I tried to point at here and I see I was not clear enough, is that a pointer or an iterator doesn't know about the container it points at. It only knows the memory address and the type that it is looking for.

Let's take a practical example. We have two containers of the same type, a and b and we have a pointer to each pa and pb. 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?

#include <iostream>
#include <array>

bool f(int* pa, int* pb) {
   // TODO
   // return true of pa and pb point at the same array, false otherwise
   return true;
}

int main() {
    int a[] = {1,2,3};
    int b[] = {4,5,6};
    int* pa = a;
    int* pb = b;
    if (f(pa,pb)) {
        std::cout << "pa and pb belong to the same array" << std::endl;
    } else {
        std::cout << "pa and pb belong to the same array" << std::endl;
    }
}

As far as I understand, this is not possible, because the pointers they don't belong to a container. And this doesn't change the fact, that it's the callers responsibility to use the pointer in a sane way not trying to depend on UB. In fact, I find it quite disturbing that the article mentions C++ whereas there is no reason in 2020 to use c-style arrays and pointer arithmetics to iterate over an array...

Thread Thread
 
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. :)