Wednesday, August 29, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...