Hi sagar
Actually its the compiler which is doing things for you.
GCC or G++ have some features that allows you to initialize array. For
example in your case 2 when you specify a single element gcc intializes the
whole array with 0. You can do this also:
Int arr [6]={[3]=0, [4]=5} p.s. gcc allows u to do this type of
initialisation.
You can refer gcc doc online for more info.
On Sep 28, 2014 3:59 PM, "sagar sindwani" <[email protected]> wrote:
> Thanks Deepak and Rahul for the reply.
>
> Do you guys have any standard document or any standard book which defines
> this? I totally agree with these answers but I don't have any formal
> written text.
>
> In my example 1, the object is on stack and this lead to a1[0].z to be
> un-initialized. But as the specified in example 2, Why every element of arr
> is initialized, it is also on the stack ? Any source to answer this
> question ?
>
> Thanks
> Sagar
>
>
>
> On Sun, Sep 28, 2014 at 2:26 PM, Rahul Vatsa <[email protected]>
> wrote:
>
>>
>> http://stackoverflow.com/questions/3127454/how-do-c-class-members-get-initialized-if-i-dont-do-it-explicitly
>>
>> On Sun, Sep 28, 2014 at 12:22 PM, Deepak Garg <[email protected]>
>> wrote:
>>
>>> Hi
>>>
>>> In example 1, member z will have a garbage value (i.e. 0 in your case )
>>>
>>> Thanks
>>> Deepak
>>> On Sep 28, 2014 11:29 AM, "sagar sindwani" <[email protected]>
>>> wrote:
>>>
>>>> I am working on How compilers handle initialization list. I came across
>>>> a case where I am not sure what should be the compiler behaviour.
>>>>
>>>> *Example 1:-*
>>>>
>>>> #include <iostream>
>>>>
>>>> class A
>>>> {
>>>> public:
>>>> int x,y,z;
>>>> };
>>>>
>>>> int main()
>>>> {
>>>> A a1[2] =
>>>> {
>>>> { 1,2 },
>>>> { 3,4 }
>>>> };
>>>>
>>>> std::cout << "a1[0].z is " << a1[0].z << std::endl;
>>>>
>>>> return 0;
>>>> }
>>>>
>>>> In above case a1[0].z is ? g++ shows it as 0 ( zero ). It is exactly 0
>>>> or garbage value, I am not sure on that.
>>>>
>>>> I tried lot of books and some documents , no where I found what C++
>>>> says for initialization of class objects.
>>>>
>>>> You can find handling of below case in almost every book.
>>>>
>>>> *Example 2:- *
>>>>
>>>> int arr[6] = {0};
>>>>
>>>> In Example 2, compilers will auto-fill all members with 0. It is
>>>> mentioned in books. But when it comes to User-defined datatypes nothing is
>>>> mentioned.
>>>>
>>>>
>>>> Please share your thoughts on this. If you find any document related to
>>>> this, please share it as well.
>>>>
>>>> Thanks
>>>> Sagar
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].