2.6 存取效率的比较
char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa是在运行时刻赋值的;
而bbbbbbbbbbb是在编译时就确定的;
但是,在以后的存取中,在栈上的数组比指针所指向的字符串(例如堆)快。
比如:
#include
void main()
{
char a = 1;
char c[] = "1234567890";
char *p ="1234567890";
a = c[1];
a = p[1];
return;
}
对应的汇编代码
10: a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4],cl
11: a = p[1];
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]
00401070 8A 42 01 mov al,byte ptr [edx+1]
00401073 88 45 FC mov byte ptr [ebp-4],al
第一种在读取时直接就把字符串中的元素读到寄存器cl中,而第二种则要先把指针值读到edx中,在根据edx读取字符,显然慢了。
2.7小结:
堆和栈的区别可以用如下的比喻来看出:
使用栈就象我们去饭馆里吃饭,只管点菜(发出申请)、付钱、和吃(使用),吃饱了就走,不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作,他的好处是快捷,但是自由度小。
使用堆就象是自己动手做喜欢吃的菜肴,比较麻烦,但是比较符合自己的口味,而且自由度大。
堆和栈的区别主要分:
操作系统方面的堆和栈,如上面说的那些,不多说了。
还有就是数据结构方面的堆和栈,这些都是不同的概念。这里的堆实际上指的就是(满足堆性质的)优先队列的一种数据结构,第1个元素有最高的优先权;栈实际上就是满足先进后出的性质的数学或数据结构。
虽然堆栈,堆栈的说法是连起来叫,但是他们还是有很大区别的,连着叫只是由于历史的原因。
http://t.163.com/event/info/eventId/-4054014659260679579 |
http://t.163.com/event/info/eventId/8716079504765824448 |
http://t.163.com/event/info/eventId/8470963592102662643 |
http://t.163.com/event/info/eventId/-2287143357252463104 |
http://t.163.com/event/info/eventId/7327815198175597698 |
http://t.163.com/event/info/eventId/5332798139198162018 |
http://t.163.com/event/info/eventId/5212677783326518562 |
http://t.163.com/event/info/eventId/-8414497406437818667 |
http://t.163.com/event/info/eventId/466022097989870039 |
http://t.163.com/event/info/eventId/-6671326893047528761 |
http://t.163.com/event/info/eventId/5988502945366661164 |
http://t.163.com/event/info/eventId/4759028504871970132 |
http://t.163.com/event/info/eventId/7466179874822061918 |
http://t.163.com/event/info/eventId/424602007495631460 |
http://t.163.com/event/info/eventId/-1112903610957972001 |
http://t.163.com/event/info/eventId/7656755762593269591 |
http://t.163.com/event/info/eventId/-2089755221267149346 |
http://t.163.com/event/info/eventId/-1466584469436639088 |
http://t.163.com/event/info/eventId/-8923116757238050825 |
http://t.163.com/event/info/eventId/-7455891012501089996 |
http://t.163.com/event/info/eventId/3228650449365324092 |
http://t.163.com/event/info/eventId/-8247692186160835118 |
http://t.163.com/event/info/eventId/-7708466573145045998 |
http://t.163.com/event/info/eventId/-6214855000495271992 |
http://t.163.com/event/info/eventId/-1220489420125055773 |
http://t.163.com/event/info/eventId/2997477298393766995 |
http://t.163.com/event/info/eventId/2575174696099955719 |
http://t.163.com/event/info/eventId/932242803760647086 |
http://t.163.com/event/info/eventId/-1117131236474314556 |
http://t.163.com/event/info/eventId/1169367759718451648 |
http://t.163.com/event/info/eventId/3961200691175027546 |
http://t.163.com/event/info/eventId/2024575377368071409 |
http://t.163.com/event/info/eventId/7588036623986839318 |
http://t.163.com/event/info/eventId/-217453328493090174 |
http://t.163.com/event/info/eventId/1464596085510983051 |
http://t.163.com/event/info/eventId/5794422413780167044 |
http://t.163.com/event/info/eventId/-8453571181253032683 |
http://t.163.com/event/info/eventId/330019483365333110 |
http://t.163.com/event/info/eventId/-8698190567575266298 |
http://t.163.com/event/info/eventId/-4145609458314693271 |
http://t.163.com/event/info/eventId/161059662820477991 |
http://t.163.com/event/info/eventId/-1668393782447264190 |
http://t.163.com/event/info/eventId/6971152366072062016 |
http://t.163.com/event/info/eventId/-5610907083493214779 |
http://t.163.com/event/info/eventId/-8725886645951000743 |
http://t.163.com/event/info/eventId/-1717648789636884507 |
http://t.163.com/event/info/eventId/3075538682082399840 |
http://t.163.com/event/info/eventId/206007153356922134 |
http://t.163.com/event/info/eventId/-8062047790743101634 |
http://t.163.com/event/info/eventId/8670838222660101995 |
http://t.163.com/event/info/eventId/-8300637193668184862 |
http://xiaozhao.renren.com/detail/experience/1377093 |
http://xiaozhao.renren.com/detail/experience/1377099 |
http://xiaozhao.renren.com/detail/experience/1377107 |
http://xiaozhao.renren.com/detail/experience/1377111 |
http://xiaozhao.renren.com/detail/experience/1377127 |
http://xiaozhao.renren.com/detail/experience/1377133 |
http://xiaozhao.renren.com/detail/experience/1377141 |
http://xiaozhao.renren.com/detail/experience/1377157 |
http://xiaozhao.renren.com/detail/experience/1377163 |
http://xiaozhao.renren.com/detail/experience/1377183 |
http://xiaozhao.renren.com/detail/experience/1377189 |
http://xiaozhao.renren.com/detail/experience/1377193 |
http://xiaozhao.renren.com/detail/experience/1377197 |
http://xiaozhao.renren.com/detail/experience/1377209 |
http://xiaozhao.renren.com/detail/experience/1377211 |
http://xiaozhao.renren.com/detail/experience/1377215 |
http://xiaozhao.renren.com/detail/experience/1377223 |
http://xiaozhao.renren.com/detail/experience/1377243 |
http://xiaozhao.renren.com/detail/experience/1377245 |
http://xiaozhao.renren.com/detail/experience/1377257 |
http://xiaozhao.renren.com/detail/experience/1377263 |
http://xiaozhao.renren.com/detail/experience/1377299 |
http://xiaozhao.renren.com/detail/experience/1377307 |
http://xiaozhao.renren.com/detail/experience/1377313 |
http://xiaozhao.renren.com/detail/experience/1377321 |
http://xiaozhao.renren.com/detail/experience/1377339 |
http://xiaozhao.renren.com/detail/experience/1377349 |
http://xiaozhao.renren.com/detail/experience/1377357 |
http://xiaozhao.renren.com/detail/experience/1377361 |
http://xiaozhao.renren.com/detail/experience/1377385 |
http://xiaozhao.renren.com/detail/experience/1377393 |
http://xiaozhao.renren.com/detail/experience/1377407 |
http://xiaozhao.renren.com/detail/experience/1377409 |
http://www.zhihu.com/collection/24896107 |
http://www.zhihu.com/collection/24896350 |
http://www.zhihu.com/collection/24896458 |
http://www.zhihu.com/collection/24896552 |
http://www.zhihu.com/collection/24896659 |
http://www.zhihu.com/collection/24896954 |
http://www.zhihu.com/collection/24897199 |
http://www.douguo.com/cookbook/444513.html |
http://www.douguo.com/cookbook/444527.html |
http://www.douguo.com/cookbook/444545.html |
http://www.douguo.com/cookbook/444562.html |
http://www.douguo.com/cookbook/444577.html |
http://www.douguo.com/cookbook/444616.html |
http://www.douguo.com/cookbook/444628.html |
http://www.douguo.com/cookbook/444643.html |
http://www.douguo.com/cookbook/444656.html |