Short answer: Magic.

Long answer: The run-time system stores the number of objects, n, somewhere where it can be retrieved if you only know the pointer, p.

Offtopic comment:
I have added the reference to the question in the Some C-C++ Programming tips thread