0
char t = 'a';
char * p1 = &t;
char** p2 = &p1;
std::cout << p2 << " " << *p2 << " " << **p2 << '\n';

I am using clang and -fsanitize=address which complains about memory access overflowing a variable and terminates my program, however when I step through with gdb I can see that yes everything is defined and it should print two addresses followed by 'a'.

If I remove the middle "*p2" it runs fine, ie it is the printing of a half-dereferenced pointer to a pointer causing me issues.

jpsalm
  • 328
  • 1
  • 8
  • Ok thanks I did not know that! – jpsalm Jun 07 '18 at 18:19
  • Possible duplicate of [cout << with char\* argument prints string, not pointer value](https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value) – Justin Jun 07 '18 at 18:20

1 Answers1

5

std::cout assumes all char* point to null terminated character strings. Cast it to void* if you want the address.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87