tags:

views:

290

answers:

7

Hi all..

In C, i have read tat half-initialized arrays will be filled with zeros for the rest of the elements (irrespective of integer or char arrays)..

Eg: 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.. am working in Code composer studio (TI's IDE).. i want to ensure that this will work for all cases and all compilers

+2  A: 

This should work irrespective of which compiler you are using.

aJ
+7  A: 

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.

Roger Pate
+10  A: 

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?

anon
A test is a good idea if you know which compilers might be used on the code. If you are concerned about portability between compilers for future work this might be more difficult ;-).
semaj
+5  A: 

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.

qrdl
Not true, see C99 6.7.8/21 "If there are fewer initializers in a brace-enclosed list than there are elements or members .. the remainder .. shall be initialized implicitly the same as objects that have static storage duration." (which, roughly, means zeroed)
Roger Pate
@Pate: Thanks for the C standards reference, Pate.. is there any standard sites where i can see the standards, for future reference
inquisitive
@Roger Yes, exactly. How my post contradicts to it?
qrdl
inquisitive: There are books that make better references when learning the language basics; there's a reason I didn't quote the standard to answer you, but did to point out the error to qrdl. Even so, you can find it at http://www.open-std.org/jtc1/sc22/wg14/www/standards.html.
Roger Pate
qrdl: Given `int arr[10] = {3};`, then `arr[4]` is not explicitly initialized and is not garbage, even when arr is an auto variable in a function.
Roger Pate
I have verified the same (auto variables half-initialization) in CCS, Visual studio and turbo C.. it was working fine for all three compilers.. my doubt is, is it standard for every compilers..
inquisitive
inquisitive: see my answer for that concern.
Roger Pate
@Roger Yes, the reminder is zeroed. That's what I've meant, and edited my post to make it more clear.
qrdl
Thanks, and you'll notice I removed my downvote and so did the other person; I think the system works. :) Perhaps it's a language issue, but I did interpret your original as clearly saying non-explicitly-initialized values would be garbage.
Roger Pate
+1  A: 

If you want to be sure that your code will work with all compilers you should initialize all your array elements just like it:

int arr[10] = {3,0,0,0,0,0,0,0,0,0};

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.

Unless you're working on an embedded project where someone has fiddled with the start-up code to remove the start-up initialisation (speaking from experience). Then all bets are off for portability!
Craig McQueen
Can get the sense of your comment. It seems you agree with my proposal. Am I right?
A: 

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

Karthik Balaguru
A: 

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.

Chris