久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 C++ 中如何確定對(duì)象的大小?

How do you determine the size of an object in C++?(在 C++ 中如何確定對(duì)象的大小?)
本文介紹了在 C++ 中如何確定對(duì)象的大小?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

例如,假設(shè)我有一個(gè) Temp 類(lèi):

For example, say I have a class Temp:

class Temp
{
    public:
        int function1(int foo) { return 1; }
        void function2(int bar) { foobar = bar; }

    private:
        int foobar;
};

當(dāng)我創(chuàng)建一個(gè) Temp 類(lèi)的對(duì)象時(shí),我將如何計(jì)算它需要多少空間,以及它在內(nèi)存中的表示方式(例如 | foobar 的 4 個(gè)字節(jié)| function1 的 8 個(gè)字節(jié) | etc | )

When I create an object of class Temp, how would I calculate how much space it needs, and how is it represented in memory (e.g.| 4 bytes for foobar| 8 bytes for function1 | etc | )

推薦答案

對(duì)于一階近似,對(duì)象的大小是其組成數(shù)據(jù)成員的大小之和.您可以確定它永遠(yuǎn)不會(huì)比這更小.

To a first order approximation, the size of an object is the sum of the sizes of its constituent data members. You can be sure it will never be smaller than this.

更準(zhǔn)確地說(shuō),編譯器有權(quán)在數(shù)據(jù)成員之間插入填充空間,以確保每個(gè)數(shù)據(jù)成員都滿(mǎn)足平臺(tái)的對(duì)齊要求.一些平臺(tái)對(duì)對(duì)齊非常嚴(yán)格,而其他平臺(tái) (x86) 更寬容,但在正確對(duì)齊的情況下會(huì)表現(xiàn)得更好.因此,即使是編譯器優(yōu)化設(shè)置也會(huì)影響對(duì)象大小.

More precisely, the compiler is entitled to insert padding space between data members to ensure that each data member meets the alignment requirements of the platform. Some platforms are very strict about alignment, while others (x86) are more forgiving, but will perform significantly better with proper alignment. So, even the compiler optimization setting can affect the object size.

繼承和虛函數(shù)增加了額外的復(fù)雜性.正如其他人所說(shuō),您的類(lèi)的成員函數(shù)本身并不占用每個(gè)對(duì)象"的空間,但是該類(lèi)接口中虛函數(shù)的存在通常意味著存在一個(gè)虛表,本質(zhì)上是一個(gè)用于查找函數(shù)指針的表動(dòng)態(tài)解析正確的函數(shù)實(shí)現(xiàn)以在運(yùn)行時(shí)調(diào)用.虛擬表(vtbl)通常通過(guò)存儲(chǔ)在每個(gè)對(duì)象中的指針訪問(wèn).

Inheritance and virtual functions add an additional complication. As others have said, the member functions of your class themselves do not take up "per object" space, but the existence of virtual functions in that class's interface generally implies the existence of a virtual table, essentially a lookup table of function pointers used to dynamically resolve the proper function implementation to call at runtime. The virtual table (vtbl) is accessed generally via a pointer stored in each object.

派生類(lèi)對(duì)象還包括其基類(lèi)的所有數(shù)據(jù)成員.

Derived class objects also include all data members of their base classes.

最后,訪問(wèn)說(shuō)明符(公共的、私有的、受保護(hù)的)在數(shù)據(jù)成員的打包方面為編譯器提供了一定的余地.

Finally, access specifiers (public, private, protected) grant the compiler certain leeway with packing of data members.

簡(jiǎn)短的回答是 sizeof(myObj) 或 sizeof(MyClass) 總是會(huì)告訴您對(duì)象的正確大小,但其結(jié)果并不總是容易預(yù)測(cè).

The short answer is that sizeof(myObj) or sizeof(MyClass) will always tell you the proper size of an object, but its result is not always easy to predict.

這篇關(guān)于在 C++ 中如何確定對(duì)象的大小?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 韩国主播午夜大尺度福利 | 日韩成人免费在线视频 | 亚洲精品永久免费 | 国产在线不卡 | 亚洲人成在线播放 | 精品区一区二区 | 中文字幕不卡在线观看 | 亚洲精品福利在线 | 精品亚洲一区二区 | 欧美日韩国产综合在线 | 亚洲免费影院 | 亚洲欧美综合 | 一区二区三区视频在线 | 精品福利在线 | 日本一区二区三区四区 | 中文字幕av在线一二三区 | 69电影网| 在线视频一区二区三区 | 国产麻豆乱码精品一区二区三区 | 香蕉一区二区 | 中文字幕一区二区不卡 | 玩丰满女领导对白露脸hd | 免费h在线 | 欧美黄视频 | 免费人成激情视频在线观看冫 | 日韩午夜精品 | 免费在线一区二区 | 久久国产精品免费一区二区三区 | 99热.com| 欧美成人一区二区三区 | 欧美性生活免费 | 国产精品久久久久一区二区三区 | www.中文字幕.com | 蜜桃黄网 | 国产精品区一区二区三 | 69热视频在线观看 | 国产精品久久久久久久模特 | 不用播放器看的av | 国产91在线视频 | 在线看片国产 | 少妇特黄a一区二区三区88av |