5
6
2014
21

sizeof函数

sizeof函数返回时所占的字节个数。同一段代码在32位机器和64位机器上的sizeof返回值可能不同。代码如下

#include"stdio.h"
struct{}str;
struct{int a;}str1;

void main(int argc,char *argv)
{
	char *string="";
	char *s="1",*s1="123456789012345",a[3]="1";	
	int i,j1,j,k,k1,k2;
	i=sizeof(string);
	j=sizeof(s);
	j1=sizeof(s1);
	k=sizeof(a);
	k1=sizeof(str);
	k2=sizeof(str1);
	printf("%d  %d  %d  %d  %d  %d\n",i,j,j1,k,k1,k2);
}

在32位机器和64位机器的运行结果如截图。注意该结果是gcc 编译c的规则的结果

 

有如下几点总结:

1.在gcc编译器下编译c,struct中间是空,则sizeof返回0。这点与下面g++编译器对比

2.char *s="124342";sizeof(s);大小与s中的内容多少无关系。都是分配了一个固定大小,即指针所占的字节数。

 

再看下面代码:gcc编译

#include"stdio.h"
struct{}str1;
struct{int a;}str2;

int main(int argc,char *argv)
{
	char *s1="";
	char *s2="1",*s3="123456789012345",s4[3]="1",s5[]="123456789";	
	int i,i1,j1,j2,j3,j4,j5,j,k,k1,k2,k3;
	i=sizeof(s1);
	j=sizeof(s2);
	j1=sizeof(s3);
	k=sizeof(s4);
	k3=sizeof(s5);//
	k1=sizeof(str1);
	k2=sizeof(str2);
	i1=sizeof(*s1);//s1为char * 类型,*s1其实就是去其指向的char,故sizeof=1,一个字节
	j2=sizeof(*s2);
	j3=sizeof(*s3);
	j4=sizeof(*s4);
	j5=sizeof(*s5);//s5为指向"123456789"中的1的指针,故sizeof结果为1字节
	printf("s1=%d  s2=%d  s3=%d  s4=%d  s5=%d  str1=%d  str2=%d\n",i,j,j1,k,k3,k1,k2);
	printf("s_1=%d  s_2=%d  s_3=%d  s_4=%d  s_5=%d\n",i1,j2,j3,j4,j5);
	return 1;
}

运行结果如下。

我们可以看出结果在注释中

 


再看如下一段cpp代码:该段代码g++编译cpp类型

#include"stdio.h"
#include <iostream>
using namespace std;
struct circle0
{	
}cir0;
struct circle1
{
	int radius;
}cir1;
struct circle2
{
	int radius;
	circle2() {radius=1.0;}
}cir2;
struct circle3
{
	int radius;
	circle3() {radius=1.0;}
	~circle3(){radius=0;}
}cir3;
struct circle4
{
	int radius;
	circle4() {radius=1.0;}
	virtual ~circle4(){radius=0;}
}cir4;
struct circle5
{
	int radius;
	circle5() {radius=1.0;}
	virtual void f1(){cout<<"f1()\n";}
	virtual ~circle5(){radius=0;}
}cir5;
struct circle6
{
	int radius;
	circle6() {radius=1.0;}
	virtual void f1(){cout<<"f1()\n";}
	virtual ~circle6(){radius=0;}
	void f2() {cout<<"f2()\n";}
}cir6;
int main()
{
	int c0,c1,c2,c3,c4,c5,c6,c_0,c_1,c_2,c_3,c_4,c_5,c_6;
	c0=sizeof(cir0);
	c1=sizeof(cir1);
	c2=sizeof(cir2);
	c3=sizeof(cir3);
	c4=sizeof(cir4);
	c5=sizeof(cir5);
	c6=sizeof(cir6);
	c_0=sizeof(circle0);
	c_1=sizeof(circle1);
	c_2=sizeof(circle2);
	c_3=sizeof(circle3);
	c_4=sizeof(circle4);
	c_5=sizeof(circle5);
	c_6=sizeof(circle6);
	printf("c0:%d  c1:%d  c2:%d  c3:%d  c4:%d  c5:%d   c6:%d\n",c0,c1,c2,c3,c4,c5,c5);
	printf("c_0:%d c_1:%d c_2:%d c_3:%d c_4:%d c_5:%d c_6:%d\n",c_0,c_1,c_2,c_3,c_4,c_5,c_6);
	
	return 1;
}

运行结果

我们可以得出如下结论

1.c编译时struct的结构如果为空,则sizeof(struct)结果为0;而c++编译则不然,结果为1

2.构造函数、析构函数、非virtual函数不占struct的空间,因为调用函数只需要知道函数的地址,而函数的地址只与类型有关,而与类型的实例无关,编译器不会因为函数而在实例中添加额外信息

3.struct中若有virtual声明的函数,则多一个指针类型的空间。因为若有虚函数,则会为该类型添加一个虚类型的表,该实例就会有一个指向虚类型的指针。故多一个指针的空间。故一个virtual函数和两个virtual函数实际上都只是多一个指针类型。

 

 


Category: 编程理解 | Tags: | Read Count: 1284
Avatar_small
Digital Ali 说:
2021年9月14日 21:35

I read that Post and got it fine and informative. 123movies

Avatar_small
SEO 说:
2021年10月01日 00:30

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. Complete Business Funding List

Avatar_small
SEO 说:
2021年10月02日 19:35

I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. basics

Avatar_small
SEO 说:
2021年10月10日 01:30 This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work รับทำ SEO ขอนแก่น
Avatar_small
SEO 说:
2021年10月18日 03:15 Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. umrah for uae residents
Avatar_small
SEO 说:
2021年11月04日 21:17

Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. 오피톡

Avatar_small
SEO 说:
2021年11月23日 19:19

KitaabNow is an online book store in pakistan with a huge variety of Academic Books and Stationery items.

Avatar_small
Umzugsfirma Zürich 说:
2021年12月24日 18:37

Die 1a umzüge GmbH ist Ihr zuverlässiger Partner für Umzug, Reinigung, Entsorgung, Lagerung und alle Dienstleistungen rund um den Umzug. Kontakt 1a Umzüge GmbH, Umzugsfirma Zürich.

Avatar_small
Website 说:
2022年6月20日 03:55

Instagram pva accounts
Yes i am totally agreed with this article and I just want say that this article is very nice and very informative article. I will make sure to be reading your blog more. You made a good point but I can't help but wonder. what about the other side? !!!!!!THANKS!!!!!!

Avatar_small
Gmail PVA Accounts 说:
2022年6月22日 00:52

Thanks for the tips and information. I really appreciate it.

Avatar_small
Website 说:
2022年7月03日 15:46

We are in the market for more than 10+ years & providing private office spaces or toronto coworking space for rent in greater toronto and covering 14+ locations. Click To Learn More!

Avatar_small
Website 说:
2022年7月03日 15:49

Marketplaceflow is the world’s most advanced Facebook Marketplace automation dropshipping software, with advanced features like automatic listing, tracking number generating, order processing, bookkeeping, and store analysis.

Avatar_small
Website 说:
2022年7月17日 03:56

Instagram PVA accounts are so easy to purchase now a days. Quickly buy as many accounts as you want and increase your business online presence on Instagram.

Avatar_small
halal vitamins 说:
2022年7月20日 20:00

Thanks for sharing the info. Keep up the good work going. I really enjoyed exploring your site. Good resource.

Avatar_small
Website 说:
2022年8月04日 01:49

Buy Instagram PVA accounts to promote your profile, brand, and business. Welcome we will provide high-quality Bulk Instagram accounts at affordable rates.

Avatar_small
mississauga notary 说:
2022年8月04日 04:34

Thanks for the post and great tips. Even, I also think that hard work is the most important aspect of getting success.

Avatar_small
Website 说:
2022年9月14日 16:05

smart home reviews
It is imperative that we read blog posts very carefully. I am already done with it and find that this post is really amazing.

Avatar_small
Sodexo Meal Card Log 说:
2022年11月08日 22:22

Simple meals can have a significant impact on your finances. Employees tend to have their tea, snacks, and lunch, which takes a portion of their salary. It’s tasking to pack meals leaving no option to purchase meals from specific food merchants. Sodexo Meal Card Login To curb the expenses, Indian employees and many others globally have the privilege to access meals through coupons (vouchers or cards).

Avatar_small
black seed oil 说:
2022年11月28日 15:05

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more posts like these. Thank you very much.

Avatar_small
truecaller en ligne 说:
2023年7月13日 20:02

Nous avons tous reçu un appel d’un numéro inconnu ou d’un appelant indésirable à un moment donné de notre vie. Et au fil du temps, le besoin de connaître l’identité de l’appelant se renforce. truecaller en ligne Le portail Truecaller fournit une recherche de numéro de téléphone en ligne sur sa plate-forme, et nous sommes là pour vous aider et c’est entièrement gratuit.

Avatar_small
MBOSE SSLC importan 说:
2023年9月14日 19:44

MBOSE SSLC Important Question Paper 2024 for Hindi, English Medium Subject Wise Available on the our Website Along with the Students Click on the Subject name Link below to Download the Paper Pdf, this MBOSE SSLC Blueprint 2024 Developed by Senior Experts only, It will help you in Analyzing your Mistakes a better way of Representing your answer in the SSLC Final Exam, Meghalaya 10th Question MBOSE SSLC important Questions Paper 2024 Paper 2024 will give Students an Exact idea about the Final Exam 2024, Students can Download the PDFs Format.Students who are Searching Internet for Meghalaya Board SSLC Model Paper 2024, Students can use These old Exam Paper as a Reference to Prepare for the Exam.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com