Wednesday 29 August 2018

c++ - Can pointers be of different sizes?




This answer comes with an interesting statement - "on machines where int* is smaller than a char*". (let's exclude pointers to functions)



Is it possible for pointers to different types to have different sizes? Why would this be useful?



Answer



Yes, it's entirely possible. On some machines, a pointer to a byte contains two values: A pointer to the WORD address of the memory word containing the byte, and a "byte index" that gives the position of the byte within the word. E.g. on a 32-bit machine, the "byte index" is 0..3.



This would require more storage space than a "int *", which is just a pointer to the relevant word.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...