指針的大小問題

問題來源:點擊打開鏈接

Also we have learned that on different systems the size of a pointercan vary. As it turns out it is also possible that the size ofa pointer can vary depending on the data type of the object towhich it points. Thus, as with integers where you can run intotrouble attempting to assign a long integer to a variable of typeshort integer, you can run into trouble attempting to assign thevalues of pointers of various types to pointer variables of othertypes.

意思是說:一個指針的大小,這裏的大小是指 指針 的長度, 也就是 sizeof(Pointer)的值,根據指針指向的數據類型的不同而不同。

微笑好像99.9%的教材講的都不一樣哦

stackoverflow: 點擊打開鏈接

The C standard allows pointers to different types to have different sizes, e.g. sizeof(char*) != sizeof(int*) is permitted. It does, however, require that if a pointer is converted to a void* and then back to its original type, it must compare as equal to its original value. Therefore, it follows logically that sizeof(void*) >= sizeof(T*) for all types T, correct?

On most common platforms in use today (x86, PPC, ARM, and 64-bit variants, etc.), the size of all pointers equals the native register size (4 or 8 bytes), regardless of the pointed-to type. Are there any esoteric or embedded platforms where pointers to different types might have different sizes? I'm specifically asking about data pointers, although I'd also be interested to know if there are platforms where function pointers have unusual sizes.

I'm definitely not asking about C++'s pointer-to-members and pointer-to-member-functions. Those take on unusual sizes on common platforms, and can even vary within one platform, depending on the properties of the pointer-to class (non-polymorphic, single inheritance, multiple inheritance, virtual inheritance, or incomplete type).



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章