C 中数组的初始化
在 C 中,我读到半初始化的数组将用零填充其余元素(无论整数或字符数组)。
例如:
int arr[10] = {3};
如果初始化,arr[4]
将为 0,如果未初始化,则为垃圾值。
我的问题是,上述内容是否适用于所有 C 编译器(或者)根据编译器选项,这种附加零可能会发生或不会发生?我在 Code Composer Studio(TI 的 IDE)工作。我想确保这适用于所有情况和所有编译器。
In C, I have read that half-initialized arrays will be filled with zeros for the rest of the elements (irrespective of integer or char arrays).
E.g.:
int arr[10] = {3};
arr[4]
will be 0 if initialized and a junk value if not initialized.
My question is, will the above work for all C compilers (or) this appending of zeros might occur or not occur depending on the compiler option? I am working in Code composer studio (TI's IDE). I want to ensure that this will work for all cases and all compilers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该行为由 C 标准指定。但如果您担心特定编译器的行为,为什么不编写一个测试呢?
The behaviour is specified by the C Standard. But if you are worried about the behaviour of your specific compiler, why not write a test?
这是根据 C 标准的,任何遵循 C 标准的编译器都必须这样做。然而,并非所有编译器都 100% 标准兼容,如果您不确定,您必须检查您的编译器是否这样做。
This is according to the C standard, and any compiler following the C standard must do this. However, not all compilers are 100% standard compliant, and you'll have to check if yours does this or not, if you're unsure.
位于数据段(全局和单元范围)中的变量会自动初始化为全零。
除非显式初始化,甚至部分初始化,否则堆栈变量(函数和块作用域)将充满垃圾。如果部分初始化,则提醒归零。
这是 C 标准,所有编译器都必须遵守它。
Variables, located in data segment (global and unit scope), are automatically initialised to all zeros.
Stack variables (function and block scope) are filled with garbage unless explicitly initialised, even partially initialised. In case of partial initialisation, reminder is zeroed.
That's by the C standard and all compilers must adhere to it.
无论您使用哪种编译器,这都应该有效。
This should work irrespective of which compiler you are using.
如果您想确保您的代码适用于所有编译器,您应该像这样初始化所有数组元素:
如果数组元素的数量太多(100 或 10000),则最好的解决方案是动态初始化它运行时。
If you want to be sure that your code will work with all compilers you should initialize all your array elements just like it:
If the number of elements of your array is too high (100 or 10000) the best solution becomes to initialize it dynamicaly at the runtime.
它取决于编译器的设计。通常编译器支持它,有些编译器可能在编译期间通过某些标志或其他选项来支持。有些人可能不支持。最好检查相关编译器支持 regd 与 C 标准的兼容性。我认为 Code Composer Studio IDE(TI DSP 处理器)支持小组可能会给您确切的答案。
卡蒂克·巴拉古鲁
It is dependent on the design of the compiler. Normally compilers support it and some may support via some flags during compilation or other options. Some may not support it. It is better to check with the concerned compiler support regd the compatibility with C standard. I think Code Composer Studio IDE(TI DSP processors) support group might give you the exact answer.
Karthik Balaguru
语言规范指定了默认行为,但并非所有编译器都实现了定义的行为。一个很好的例子是,Visual Studio 2008 是 Microsoft C/C++ 编译器的第一个版本,它将对数组中未初始化的元素调用默认构造函数,这是至少自 C++ 98 规范以来已定义的数组初始化行为。
如果您担心代码在多个编译器上运行时的行为方式,最好是安全起见,并显式初始化所有值。
The language spec specifies default behavior, however not all compilers implement the defined behavior. A good example of this is that Visual Studio 2008 is the first version of the Microsoft C/C++ compiler that will call the default constructor on uninitialized elements in an array which has been the defined behavior of array initialization since at least the C++ 98 spec.
If you are worried about how your code will behave running on multiple compilers it is better to be safe than sorry and explicitly initialize all values.