初始化数组在C ++中 [英] Initialising arrays in C++
本文介绍了初始化数组在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我到处看看还有谁认为这叫喊变量未初始化是坏,我当然同意并理解为什么人们 - 但是,我的问题是,是否有场合,你不会想这样做?
Everywhere I look there are people who argue vociferously that uninitialised variables are bad and I certainly agree and understand why - however; my question is, are there occasions when you would not want to do this?
例如,以code:
char arrBuffer[1024] = { '\0' };
是否归零整个阵列创造超过使用数组初始化没有它的性能影响?
Does NULLing the entire array create a performance impact over using the array without initialising it?
推荐答案
我假设一个堆栈初始化,因为静态数组是自动初始化。结果
G ++输出
I assume a stack initialization because static arrays are auto-initialized.
G++ output
char whatever[2567] = {'\0'};
8048530: 8d 95 f5 f5 ff ff lea -0xa0b(%ebp),%edx
8048536: b8 07 0a 00 00 mov $0xa07,%eax
804853b: 89 44 24 08 mov %eax,0x8(%esp)
804853f: c7 44 24 04 00 00 00 movl $0x0,0x4(%esp)
8048546: 00
8048547: 89 14 24 mov %edx,(%esp)
804854a: e8 b9 fe ff ff call 8048408 <memset@plt>
所以,你初始化{'\\ 0'}和memset的呼叫完成的,所以是的,你有性能损失。
So, you initialize with {'\0'} and a call to memset is done, so yes, you have a performance hit.
这篇关于初始化数组在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文