小区别,大容量 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
重温K&R(4)

小区别,大容量

daybreakcx posted @ 2010年7月24日 03:43 in 学习笔记 with tags 语法糖 汇编化 , 3120 阅读

大多数编程语言都存在称为“语法糖”的东西,实际上是为了代码编写方便而提供的另一种表达形式,但是这些东西一定是甜蜜的吗?最近发现了一个东西,来给大家分享一下。

为了突出重点,将不必要的部分省去,列出下面两段代码:

 

/*
 * try_1.c
 */
int a[50000] = {1};
int main()
{
	return 0;
}

 

 

/*
 * try_2.c
 */
int a[50000];
int main()
{
	a[0] = 1;
	return 0;
}

 

对于数组的初始化操作就是我们今天要提及的语法糖,看起来两段代码是相同的,但是实际上他们有着微妙的差别,让我们来编译一下,然后比较看看:

 

[daybreakcx@Fedora ~]$ gcc -o a1 try_1.c
[daybreakcx@Fedora ~]$ gcc -o a2 try_2.c
[daybreakcx@Fedora ~]$ ls -l a*
-rwxrwxr-x 1 daybreakcx daybreakcx 207417  7月 23 19:48 a1
-rwxrwxr-x 1 daybreakcx daybreakcx   7377  7月 23 19:48 a2

 

注意看大小一列,看出来了吗,虽然同为全局变量,但是两个大小差了200000个字节以上,再看看我们的数组大小是50000,根据每个int是4字节来算,大小就差不多了,在此进行猜测:也许第二个程序并没有将a数组分配在二进制文件中,而第一个程序却这么干了,于是我们生成一下汇编代码来看看:

 

[daybreakcx@Fedora ~]$ gcc -S try_1.c
[daybreakcx@Fedora ~]$ gcc -S try_2.c

 

先是第一个程序的:

 

[daybreakcx@Fedora ~]$ cat try_1.s
	.file	"try_1.c"
.globl a
	.data
	.align 32
	.type	a, @object
	.size	a, 200000
a:
	.long	1
	.zero	199996
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	movl	$0, %eax
	popl	%ebp
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 4.4.4 20100630 (Red Hat 4.4.4-10)"
	.section	.note.GNU-stack,"",@progbits

 

接着是第二个:

 

[daybreakcx@Fedora ~]$ cat try_2.s
	.file	"try_2.c"
	.comm	a,200000,32
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	movl	$1, a
	movl	$0, %eax
	popl	%ebp
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 4.4.4 20100630 (Red Hat 4.4.4-10)"
	.section	.note.GNU-stack,"",@progbits

 

看到了吗,第一个程序将整个数组分配在了.data段之中,这也就是出现上述现象的原因,这是个编译器行为,本机没有windows系统,所以cl无法测试,但是对于在gcc下的童鞋发出提示:当数值个数不是很多的时候,不选用第一个程序那种初始化方式也许会更好一些,比如在嵌入式环境中(因为都是全局数组,所以其他部分都是会初始化为0的,两者并没有区别)。

那么是不是但凡这样的初始化方式都会导致这样的情况发生呢?我们是否该杜绝使用它呢?看下面这个例子:

 

/*
 * try_3.c
 */
int a[50000] = {0};
int main()
{
	return 0;
}

 

然后是编译看信息:

 

[daybreakcx@Fedora ~]$ gcc -o a3 try_3.c
[daybreakcx@Fedora ~]$ ls -l a3
-rwxrwxr-x 1 daybreakcx daybreakcx 7377  7月 23 20:03 a3

 

很显然和第二个程序的大小相同,那是否与第二个程序进行了同样的处理呢?我们来看看它的汇编代码再下结论:

 

[daybreakcx@Fedora ~]$ gcc -S try_3.c
[daybreakcx@Fedora ~]$ cat try_3.s
	.file	"try_3.c"
.globl a
	.bss
	.align 32
	.type	a, @object
	.size	a, 200000
a:
	.zero	200000
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	movl	$0, %eax
	popl	%ebp
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 4.4.4 20100630 (Red Hat 4.4.4-10)"
	.section	.note.GNU-stack,"",@progbits

 

很显然,它的处理和第一个程序相同,不同的是它分配在了.bss段,在其中是不事先分配在二进制文件之中的,也就是说根据第一个程序的定义语法,会有两种不同的结果,根据是否有非0值,可能是.data或者.bss两者之一,而分配在.data段中会产生的结果就是我们先前见到的,而第二个程序中单独定义全局数组的方式则不在二进制文件中进行分配。

结论:语法糖给我们带来了方便,很多人也很喜欢使用,但是使用的效果因人而异,而这些差异在于大家的熟练程度和认知程度,平时多反反程序,对于理解很有帮助,至于随意使用的后果嘛,没啥好说的,大家看到了什么就是什么,我就不予总结了,发完文章了,闪人…………

 

PS:多谢网友提醒,造成不同行为的语法的确不能称为语法糖,原文内容就不做修改了,数组初始化的语法影响了编译器行为,说它是语法糖是错误的,特在此编辑说明。

Crane 说:
2010年7月26日 00:58

你主要讲了全局变量初始化为不为0的值的话会放在data段,不初始化或初始化为0值会放在bss段,从而程序大小不一样。
这个是程序编译链接时的问题,这个不叫语法糖吧?就像你的定义,语法糖说的是语法层面的东西,如果一个语法去掉了对整个系统没有什么影响,那么它可以称为是一个语法糖,为了方便书写而来,比如for语句就可以说是一个语法糖。

Avatar_small
daybreakcx 说:
2010年7月29日 02:48

多谢指正,的确造成副作用的是不能叫做语法糖,我将文章修改了:)

Avatar_small
纵横天下 说:
2010年8月04日 07:10

哦,这事我以前遇见过,我在想所有的数组是不是都应该动态分配,好处是可以减小文件大小,坏处是容易内存泄漏,降低程序运行速度,弊大于利,还是不该的,如果非常大的话还可以

KVS 5th Class Model 说:
2022年9月27日 21:00

Subject experts of Kendriya Vidyalaya Sangathan have designed and provided the subject wise latest model paper with suggested answer solutions to the Elementary Level Primary School education STD-5 student located in all regions of the country, KVS 5th Class Model Paper and the KVS 5th Class Model Paper 2023 Download available various private websites also.Subject experts of Kendriya Vidyalaya Sangathan have designed and provided the subject wise latest model paper with suggested answer solutions to the Elementary Level Primary School education STD-5 student located in all regions of the country.

civaget 说:
2023年12月11日 03:48

My organic traffic has surged since partnering with 백링크하이. SEO success story!

civaget 说:
2023年12月12日 19:38

제주유흥's karaoke rooms are the best I've experienced. Fun times with friends!

civaget 说:
2023年12月12日 20:38

I had the best 러시아마사지 session at home. Convenience and quality, what more could you ask for?

civaget 说:
2023年12月12日 21:53 강남출장마사지 is a haven of relaxation in Seoul's bustling heart. With a diverse team and personalized services, it's the ultimate destination for rejuvenation.
civaget 说:
2023年12月14日 23:57

Wonderful article, thank you for sharing the info. It isn’t too often that you simply read articles where the poster understands what they’re blogging about. Grammar as well as punctuational are spot on too, only trouble I seemed to have had been bringing up the site, appeared sluggish. Looks like other visitors experienced the same trouble? NBA중계

civaget 说:
2023年12月16日 21:03

Elevate your travel experience with a stay at a top-tier 휴게텔, where luxury meets serenity.

civaget 说:
2023年12月19日 01:31 The absence of intrusive ads on 누누티비 enhances the viewing experience.
civaget 说:
2023年12月23日 15:46

Tipping's versatility in 설문조사 사이트 추천 sets them apart.

civaget 说:
2023年12月23日 18:05

오피's rise in popularity is intriguing. Its diverse offerings cater to various preferences, providing a unique escape.

civaget 说:
2023年12月26日 22:08

I thought it was heading to become some dull previous publish, however it truly compensated for my time. I’ll publish a hyperlink to this web page on my blog. I am positive my visitors will uncover that extremely helpful. 에볼루션카지노q

civaget 说:
2023年12月26日 23:05

I've introduced my family to무료스포츠중계, and they can't get enough. It's a family affair now.

civaget 说:
2023年12月27日 04:34

The ability to experiment with different pricing strategies during promotions or sales is a key advantage. self publishing a book

civaget 说:
2023年12月28日 22:40

Exquisite massages at 창원휴게텔 left me relaxed and rejuvenated. A must-visit for anyone seeking blissful tranquility.

civaget 说:
2023年12月29日 18:40

티비위키 keeps me connected with fellow entertainment enthusiasts.

civaget 说:
2024年1月01日 13:35

How much of an helpful document, hold publishing special someone Divine Revelations

civaget 说:
2024年1月02日 18:53

YouTube better watch out for 폰허브.

먹튀폴리스신고 说:
2024年3月04日 13:41

Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!..This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog..I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You.I really loved reading your blog. It was very well authored and easy to understand.

온라인카지노추천 说:
2024年3月04日 13:50

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? !!!!!!ThanksStopping by your blog helped me to get what I was looking for.

토토사이트추천 说:
2024年3月04日 14:04

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too.I guess I am not the only one having all the enjoyment here keep up the good work

토동산 说:
2024年3月04日 14:11

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too.Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.

먹튀검증 说:
2024年3月04日 14:22

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

먹튀검증백과 说:
2024年3月04日 14:29

Wow i can say that this is another great article as expected of this blog.Bookmarked this site..You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming.I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.

먹튀사이트 说:
2024年3月04日 14:32

The Brad Hendricks Law Firm has the TRADITION of working hard to resolve the legal problems faced by individuals and businesses throughout the state of Arkansas. We also have the REPUTATION of devoting the time and attention your case deserves in order to win your case. We draw upon over 275 years of combined experience and RESULTS-driven success to represent you in a range of legal needs. It is our service that has made The Brad Hendricks Law Firm one of the most successful law firms in the state for over twenty-five years.

슬롯나라 说:
2024年3月04日 15:07

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

토토검증커뮤니티 说:
2024年3月04日 15:08

Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.

토토사이트 说:
2024年3月04日 15:08

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

먹튀사이트 说:
2024年3月04日 15:54

Wow, What a Excellent submit. I certainly observed this to an awful lot informatics. It is what i used to be attempting to find.I would love to indicate you that please preserve sharing such sort of info.Thanks

메이저사이트 说:
2024年3月04日 15:54

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

온라인카지노 说:
2024年3月04日 16:43

There are very a great deal of details this way to take into consideration. That is a excellent denote bring up. I provde the thoughts above as general inspiration but clearly you will find questions such as the one you talk about the location where the most important thing will likely be in honest excellent faith. I don?t know if guidelines have emerged about items like that, but Almost certainly that a job is clearly defined as a good game. Both kids feel the impact of only a moment’s pleasure, for the remainder of their lives.

먹튀사이트조회 说:
2024年3月04日 16:43

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well!

먹튀검증사이트 说:
2024年3月04日 16:44

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work.I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed...Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanksi never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.

안전카지노사이트 说:
2024年3月04日 17:01

Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards

baccarat friends 说:
2024年3月04日 17:02

Needed to compose you a tiny note to finally thank you very much yet again for your personal splendid methods you have discussed above. It is strangely open-handed with people like you to provide publicly all that a number of people would have marketed as an electronic book to generate some bucks for their own end, primarily now that you could possibly have tried it if you ever wanted. These inspiring ideas likewise acted like a fantastic way to know that the rest have the same dreams really like my personal own to see a whole lot more concerning this problem. I’m sure there are thousands of more enjoyable times in the future for many who check out your blog.

토토사이트추천 说:
2024年3月04日 17:02

Trying to say thank you won't simply be adequate, for the astonishing lucidity in your article. I will legitimately get your RSS to remain educated regarding any updates. Wonderful work and much accomplishment in your business endeavors

먹튀검증커뮤니티 说:
2024年3月04日 17:58

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too.I guess I am not the only one having all the enjoyment here keep up the good work

토토사이트 说:
2024年3月04日 18:01

Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!..This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog..I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You.I really loved reading your blog. It was very well authored and easy to understand.

토토팡 说:
2024年3月04日 18:01

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too.I guess I am not the only one having all the enjoyment here keep up the good work

토토사이트추천 说:
2024年3月04日 18:01

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? !!!!!!ThanksStopping by your blog helped me to get what I was looking for.

카지노프렌즈 说:
2024年3月04日 19:12

Wow i can say that this is another great article as expected of this blog.Bookmarked this site..You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming.I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.

메이저사이트목록 说:
2024年3月04日 19:13

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

먹튀검증 说:
2024年3月04日 19:13

My business is looking to find in advance designed for this specific useful stuffs… I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...

토토사이트순위 说:
2024年3月04日 19:31

Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.

먹튀신고 说:
2024年3月04日 19:31

The Brad Hendricks Law Firm has the TRADITION of working hard to resolve the legal problems faced by individuals and businesses throughout the state of Arkansas. We also have the REPUTATION of devoting the time and attention your case deserves in order to win your case. We draw upon over 275 years of combined experience and RESULTS-driven success to represent you in a range of legal needs. It is our service that has made The Brad Hendricks Law Firm one of the most successful law firms in the state for over twenty-five years.

온라인사이트추천 说:
2024年3月04日 19:32

Wow i can say that this is another great article as expected of this blog.Bookmarked this site..You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming.I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.

먹튀검증 说:
2024年3月04日 20:50

Wow, What a Excellent submit. I certainly observed this to an awful lot informatics. It is what i used to be attempting to find.I would love to indicate you that please preserve sharing such sort of info.Thanks

먹튀검증사이트 说:
2024年3月04日 20:51

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

먹튀사이트조회 说:
2024年3月04日 20:51

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work.I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed...Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanksi never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.

먹튀검증업체 说:
2024年3月04日 20:54

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well!

메이저안전놀이터 说:
2024年3月04日 20:55

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..This is just the information I am finding everywhere.Admiring the time and effort you put into your blog and detailed information you offer!..Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

먹튀검증커뮤니티 说:
2024年3月04日 20:55

There are very a great deal of details this way to take into consideration. That is a excellent denote bring up. I provde the thoughts above as general inspiration but clearly you will find questions such as the one you talk about the location where the most important thing will likely be in honest excellent faith. I don?t know if guidelines have emerged about items like that, but Almost certainly that a job is clearly defined as a good game. Both kids feel the impact of only a moment’s pleasure, for the remainder of their lives.

안전토토사이트 说:
2024年3月04日 21:13

Trying to say thank you won't simply be adequate, for the astonishing lucidity in your article. I will legitimately get your RSS to remain educated regarding any updates. Wonderful work and much accomplishment in your business endeavors


登录 *


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