pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

C++ 中帶有 int 、函數、虛函數的 sizeof 類?

sizeof class with int , function, virtual function in C++?(C++ 中帶有 int 、函數、虛函數的 sizeof 類?)
本文介紹了C++ 中帶有 int 、函數、虛函數的 sizeof 類?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是一道在線C++試題,已經做完了.

This is an online C++ test question, which has been done.

#include<iostream>
using namespace std; 
class A
{

};
class B
{
int i; 
}; 

class C
{
void foo();
};
class D
{
virtual void foo();
};

class E
{
int i ; 
    virtual void foo();
};
class F
{
int i; 
    void foo();
};
class G
{
    void foo();
    int i;
    void foo1();
};

class H
{
    int i ;
    virtual void foo();
    virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << endl ;
cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ;
cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ;
cout <<"sizeof(class D) after making foo virtual : " << sizeof(D) << endl ;
cout <<"sizeof(class E) after adding foo virtual , int : " << sizeof(E) << endl ;
cout <<"sizeof(class F) after adding foo  , int : " << sizeof(F) << endl ;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(G) << endl ;
G g;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(g) << endl ;
cout <<"sizeof(class H) after adding int 2 virtual " << sizeof(H) << endl ;
return 0; 
}

輸出:

sizeof(class A) : 1
sizeof(class B) adding the member int i : 4
sizeof(class C) adding the member void foo() : 1
sizeof(class D) after making foo virtual : 8
sizeof(class E) after adding foo virtual , int : 16
sizeof(class F) after adding foo  , int : 4
sizeof(class G) after adding foo   , unsigned int : 4
sizeof(class g) after adding foo  , unsigned int : 4
sizeof(class H) after adding int 2 virtual 16

我的問題:

為什么 siszeof(A) 是 1 而 sizeof(C) 也是 1?

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

為什么 siszeof(H) 是 16 而 sizeof(G) 是 4 ?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

為什么 siszeof(E) 是 16 而 sizeof(F) 是 4 ?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

為什么 siszeof(D) 是 8 而 sizeof(E) 是 16 ?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

我的猜測:

虛函數是一個8字節的指針.但是,我不知道為什么 E 大小是 16 ?向空類添加函數不會改變其大小?

A virtual function is a pointer with 8 bytes. But, I do not know why E size is 16 ? Adding a function to an empty class does not change its size ?

感謝任何幫助.

謝謝

推薦答案

首先,虛函數不是一個 8 字節的指針.在 C++ 中,只有 sizeof(char) 保證是任意數量的字節.

First off, a virtual function is not a pointer with 8 bytes. In C++ nothing but sizeof(char) is guaranteed to be any number of bytes.

第二,只有類中的第一個虛函數會增加其大小(依賴于編譯器,但在大多數情況下 - 如果不是全部 - 就像這樣).所有后續方法都沒有.非虛函數不影響類的大小.

Second, only the first virtual function in a class increases its size (compiler-dependent, but on most - if not all - it's like this). All subsequent methods do not. Non-virtual functions do not affect the class's size.

發生這種情況是因為類實例不保存指向方法本身的指針,而是指向虛擬函數表,每個類一個.

This happens because a class instance doesn't hold pointers to methods themselves, but to a virtual function table, which is one per class.

如果你有:

class A
{
   virtual void foo();
}

class B
{
   virtual void goo();
   virtual void test();
   static void m();
   void x();
}

你會有 sizeof(A) == sizeof(B).

現在:

為什么 siszeof(A) 是 1 而 sizeof(C) 也是 1?

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

AC 的大小為 1 只是因為不允許類的大小為 0.函數與它無關.這只是一個虛擬字節.

A and C have size 1 just because it's not allowed for a class to be of size 0. The functions have nothing to do with it. It's just a dummy byte.

為什么 siszeof(H) 是 16 而 sizeof(G) 是 4 ?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

G 只有一個成員占內存 - int.在你的平臺上,sizeof(int) == 4.H,除了int,還有一個指向vftable(虛函數表,見上)的指針.this 的大小、int 的大小和對齊方式是特定于編譯器的.

G has only one member that accounts for memory - the int. And on your platform, sizeof(int) == 4. H, besides the int, also has a pointer to the vftable (virtual function table, see above). The size of this, size of int and allignment are compiler specific.

為什么 siszeof(E) 是 16 而 sizeof(F) 是 4 ?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

上面解釋了 - 非虛方法不占用類中的內存.

Explained above - non virtual methods don't take up memory in the class.

為什么 siszeof(D) 是 8 而 sizeof(E) 是 16 ?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

D 僅包含 vftable 指針,它在您的平臺上顯然是 8 個字節.E 也有一個 int,vftable 對齊到 8 個字節.所以它是這樣的:

D only contains the vftable pointer which is apparently 8 bytes on your platform. E also has an int, and the vftable is aligned to 8 bytes. So it's something like:

class E

4 bytes for int |  4 padding bytes  |  8 bytes for vftable pointer  | 
| x | x | x | x |    |    |    |    | v | v | v | v | v | v | v | v |

這篇關于C++ 中帶有 int 、函數、虛函數的 sizeof 類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | WF2户外三防照明配电箱-BXD8050防爆防腐配电箱-浙江沃川防爆电气有限公司 | 成都中天自动化控制技术有限公司| 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 北京公司注册_代理记账_代办商标注册工商执照-企力宝 | 隧道烘箱_隧道烘箱生产厂家-上海冠顶专业生产烘道设备 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 旋片真空泵_真空泵_水环真空泵_真空机组-深圳恒才机电设备有限公司 | 志高装潢官网-苏州老房旧房装修改造-二手房装修翻新 | 成都APP开发-成都App定制-成都app开发公司-【未来久】 | 卫生型双针压力表-高温防腐差压表-安徽康泰电气有限公司 | 海南在线 海南一家 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 家德利门业,家居安全门,别墅大门 - 安徽家德利门业有限公司 | 上海律师咨询_上海法律在线咨询免费_找对口律师上策法网-策法网 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 碳刷_刷握_集电环_恒压簧_电刷厂家-上海丹臻机电科技有限公司 | 液压中心架,数控中心架,自定心中心架-烟台恒阳机电设计有限公司 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 | 丹尼克尔拧紧枪_自动送钉机_智能电批_柔性振动盘_螺丝供料器品牌 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | 真空冷冻干燥机_国产冻干机_冷冻干燥机_北京四环冻干 | 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 | 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | b2b网站大全,b2b网站排名,找b2b网站就上地球网 | 四川职高信息网-初高中、大专、职业技术学校招生信息网 | 烟台金蝶财务软件,烟台网站建设,烟台网络推广 | 升降炉_真空气氛炉_管式电阻炉厂家-山东中辰电炉有限公司 | 贴板式电磁阀-不锈钢-气动上展式放料阀-上海弗雷西阀门有限公司 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 | Pos机办理_个人商户免费POS机申请-拉卡拉办理网 | 氧化锆纤维_1800度高温退火炉_1800度高温烧结炉-南京理工宇龙新材料股份有限公司 |