There is one way, however, it is intolerable and will only work on Linux. Need to look at the table of memory allocated to the process. If the memory to which the pointer points is marked as readable, it means that the pointer can be dereferenced and output the contents of that memory. If you need to write something in this memory, you should look at the second flag.
Of course, this option is more suitable only for debugging purposes. But, perhaps, on other systems, you can somehow find out the correctness of the pointer.
The idea of the code was taken from the source of the pmap utility. More information about the format of the file /proc/self/maps can be found in the reference manual proc (5) .
bool can_i_read(void* p) { uintptr_t begin, end; char readable, writable, executable, mapped; FILE* fp = fopen("/proc/self/maps", "r"); if (!fp) { return false; } while (fscanf(fp, "%" SCNxPTR "-%" SCNxPTR " %c%c%c%c", &begin, &end, &readable, &writable, &executable, &mapped) == 6) { if (begin <= (uintptr_t)p && (uintptr_t)p < end) { fclose(fp); // если нужно проверить доступность на запись, // следует смотреть флаг writable return readable == 'r'; } // не зациклимся — перед концом всегда будет перевод строки while (fgetc(fp) != '\n') ; } fclose(fp); return false; }
Performance tested on Ubuntu 14.04.4 / Linux 4.1.15