Re: [algogeeks] C++ initialization list
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 sindwani.sa...@gmail.com 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com.
Re: [algogeeks] C++ initialization list
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 deepakgarg...@gmail.com 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 sindwani.sa...@gmail.com 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com.
Re: [algogeeks] C++ initialization list
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 vatsa.ra...@gmail.com 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 deepakgarg...@gmail.com 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 sindwani.sa...@gmail.com 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com.
Re: [algogeeks] C++ initialization list
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 sindwani.sa...@gmail.com 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 vatsa.ra...@gmail.com 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 deepakgarg...@gmail.com 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 sindwani.sa...@gmail.com 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com.
Re: [algogeeks] C++ initialization list
Here you go http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3337.pdf The c++ standard itself. Refer to section 8.5.4 page no. 213. Looks like even this int a[10] = {2} is not guaranteed to initialize all the elements of the array. Sure gcc provides this but then it becomes a compiler specific thing. The language doesn't advocates it. Saurabh Singh B.Tech (Computer Science) MNNIT blog:geekinessthecoolway.blogspot.com On Sun, Sep 28, 2014 at 3:47 PM, sagar sindwani sindwani.sa...@gmail.com 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 vatsa.ra...@gmail.com 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 deepakgarg...@gmail.com 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 sindwani.sa...@gmail.com 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com.
Re: [algogeeks] C++ initialization list
Hi Saurabh Thanks for the document. Please refer to start of page 214, Section 8.5.4 ,point 3, Below is example from that struct S2 { int m1; double m2, m3; }; S2 s21 = { 1, 2, 3.0 }; // OK S2 s22 { 1.0, 2, 3 }; error: narrowing S2 s23 { }; // OK: default to 0,0,0 I tried the above case with valgrind, even valgrind had not shown any un-initialized read. Document also says it is incomplete and incorrect. Thanks Sagar On Sun, Sep 28, 2014 at 4:41 PM, saurabh singh saurab...@gmail.com wrote: Here you go http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3337.pdf The c++ standard itself. Refer to section 8.5.4 page no. 213. Looks like even this int a[10] = {2} is not guaranteed to initialize all the elements of the array. Sure gcc provides this but then it becomes a compiler specific thing. The language doesn't advocates it. Saurabh Singh B.Tech (Computer Science) MNNIT blog:geekinessthecoolway.blogspot.com On Sun, Sep 28, 2014 at 3:47 PM, sagar sindwani sindwani.sa...@gmail.com 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 vatsa.ra...@gmail.com 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 deepakgarg...@gmail.com 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 sindwani.sa...@gmail.com 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com. -- 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 algogeeks+unsubscr...@googlegroups.com.