3

You might feel code is long and I write lot of things but believe me it is quite simple.

I referred these two answers on stack-overflow for understanding this. post1 & post2

Code

#include <iostream>

class A
        {
        public:
            int k1=3;
            float f1=34.20;
            char ch1='a';
            std::string str1="bonaparte";

            void display()
            {
                std::cout<<k1<<" "<<f1<<" "<<ch1<<" "<<str1<<" xx\n";
            }

        };

class B
{
    public:
        int k2;
        float f2;
        char ch2;
        std::string str2;

        void display()
        {
            std::cout<<k2<<" "<<f2<<" "<<ch2<<" "<<str2<<" xx\n";
        }

};

A a1;
B b1;

A *p1=new A;
B *q1=new B;
int main()
{
    A a2, a3{};
    B b2, b3{};

    A *p2=new A;
    A *p3=new A{};
    A *p4=new A();

    B *q2=new B;
    B *q3=new B{};
    B *q4=new B();

    a1.display();
    a2.display();
    a3.display();
    p1->display();
    p2->display();
    p3->display();
    p4->display();

    std::cout<<"\n\n";

    b1.display();
    b2.display();
    b3.display();
    q1->display();
    q2->display();
    q3->display();
    q4->display();


}

Output

3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx


0 0    xx
13965136 0 ╠  xx
0 0    xx
38872208 0 p  xx
38872208 0 p  xx
0 0    xx
0 0    xx

Class B

b1 is value initialized precisely zero initialized.

b2 is default initialized means no initialized.

b3 is brace initialized means value initialized which leads to zero initialized.

I am not able to create b4() like q4 (not exact like but you know what I mean) as it leads function definition rather than declaration of object( Most vexing parse)

I think q1 should value initialized but after seeing output it's not matches what I think.

q2 is default initialized means no initialized.

q3 is brace initialized means value initialized which leads to zero initialized.

q4 calls constructor but there is no user defined constructor so it should default initialized means no initialized.

I know q1, q2, q3, q4 are pointer but just to easy I am using above language for ex. q3 is brace initialized. but I think you know what I mean.

What I believe regarding b1, b2, b3 turns out to be true so I don't have any question regarding that might be it is just mere coincidence so correct me if I am wrong.

Regarding q1, q2, q3, q4

what I assume for b1 I think it should work for q1 too but it not happens so please explain that.

Regarding q2 what I think matches so no problem but still correct me if I am wrong.

Now there is confusion with q3 & q4. I think both are calling for constructors but I am not sure. I assume both calling constructor but we have not user defined constructors so it should default initialized means un-initialized isn't that right ? so how come both are value initialized precisely zero initialized.

Class A

So if we do in-class initialization no matter where we declare object it's going to initialize with what we provide, I feel it's right but just want to confirm and please add if you would like to tell me bit more about this.

Abhishek Mane
  • 619
  • 7
  • 20
  • https://godbolt.org/z/qKnGx6W4f only `b2.display()` shows uninitialized members – Sergey Kolesnik Dec 06 '21 at 13:06
  • 1
    _"if we do in-class initialization no matter where we declare object it's going to initialize with what we provide"_ It's generally not true. You can "override" this initialization in custom-defined constructors. Live demo: https://godbolt.org/z/779Tc5Pc7. – Daniel Langr Dec 06 '21 at 13:12
  • `q3` and `q4` are both [_value-initialized_](https://en.cppreference.com/w/cpp/language/value_initialization). In case of `B`, default-initialization results in _zero-initialization_. – Daniel Langr Dec 06 '21 at 13:18
  • 1
    @DanielLangr so why is global `b1` and `q2` end up zero-initialized, though `b2` - not? – Sergey Kolesnik Dec 06 '21 at 13:22
  • AFAIK, objects with static storage duration are _zero-initialized_, which is the case of `b1`. As for `*q2`, its `k2`, `f2` and `ch2` have undetermined values. Which may or may not be 0. – Daniel Langr Dec 06 '21 at 13:31
  • 2
    `This question needs debugging details. It is not currently accepting answers.` it seems like the question has been closed for an invalid reason – Sergey Kolesnik Dec 06 '21 at 13:32
  • Note that this question (plus your comments) asks many questions at once. You should better ask only a single question per question. With the minimal code; the code here is very long for a single question. – Daniel Langr Dec 06 '21 at 13:48
  • @SergeyKolesnik first of all thanks. regarding your 2nd comment `only b2.display() shows uninitialized members`. what's about `q1` & `q2` I think both are un-initialized aren't they ? – Abhishek Mane Dec 06 '21 at 17:19
  • @DanielLangr Thanks I got your first comment but I think your 2nd and 3rd comment are contradictory to each other. – Abhishek Mane Dec 06 '21 at 17:21
  • @DanielLangr Regarding your last comment I know it's too much stuff at once but I also don't want to ask such that but to get understand difference among all these initialization types I have to ask that's why ? if you feel I am right please reopen. – Abhishek Mane Dec 06 '21 at 17:23
  • @AbhishekMane godbolt shows otherwise. Look at the link in the first comment – Sergey Kolesnik Dec 06 '21 at 17:25
  • @SergeyKolesnik yes I checked link but how come it is possible different outputs for your compiler even I am using `CLion` IDE with migw64 version 8.1 for C++23 still same output which I showed in Question – Abhishek Mane Dec 06 '21 at 17:39
  • @SergeyKolesnik please reply. – Abhishek Mane Dec 07 '21 at 05:46
  • @AbhishekMane It's a bit more complicated. `B` has an implicitly-defined default constructor which is _non-trivial_ due the the non-trivial default constructor of `B` subobject of type `std::string`. This default constructor of `B` does not initialize members `k1`, `f1`, and `ch1`. In `B *q1=new B;`, `B b2;`, and `B *q2=new B;`, only this constructor is used and those members, therefore, have unspecified values in all mentioned cases. Note that unspecified may be zeros as well. – Daniel Langr Dec 07 '21 at 07:16
  • @AbhishekMane However, if you write `B b3{};`, `B *q3=new B{}`, and `B *q4=new B();`, then so-called _zero-initialization_ is performed first, since the default constructor of `B` is neither custom-defined nor deleted. This zeros-out the members `k1`, `f1`, and `ch1`. Then, the default constructor is called since it is non-trivial (see https://en.cppreference.com/w/cpp/language/value_initialization). With `B b1;`, the situation is effectively the same since it is a variable with _static storage duration_. (BTW, I voted for reopen.) – Daniel Langr Dec 07 '21 at 07:25
  • @AbhishekMane Also note that reading _indetermined (unspecified) values_ results in _undefined behavior_ with only few exceptions (`unsigned char` or `std::byte`), which do not apply here. See https://en.cppreference.com/w/cpp/language/default_initialization. Your program, therefore, does not have defined behavior. – Daniel Langr Dec 07 '21 at 07:28
  • You can see the difference here where with _value-initialization_, the `int` member is additionally zeroed: https://godbolt.org/z/f6Maed8j7. With _default-initialization_, it is not. – Daniel Langr Dec 07 '21 at 07:59
  • @AbhishekMane I think it deserves a question of its own – Sergey Kolesnik Dec 07 '21 at 09:35
  • @DanielLangr https://godbolt.org/z/sWa1z4K4j see here I removed `std::string` from `class B` still `b2` is giving unspecified value ? and in my `CLion` ide with mingw64 all `b2`, `q1`, `q2` are unspecified. So how come you are telling that default initialization results in zero initialization in case of `class B`. Also please note by mistaken in your first comment in todays reply you talked about class B and used `k1`, `f1`... instead of `k2`, `f2` ... – Abhishek Mane Dec 07 '21 at 11:51
  • @DanielLangr also what happen if class (`x`) contain object of another class(`y`) which has user-defined constructor. So assume former class(`x`) has no user provided constructor so what's it causes to default initialize all the members of class(`x`) can you tell bit about that – Abhishek Mane Dec 07 '21 at 11:56
  • @AbhishekMane You are asking about many different cases. Effects of all types of initialization are in detail described in the documentation. See https://en.cppreference.com/w/cpp/language/default_initialization, https://en.cppreference.com/w/cpp/language/value_initialization, and https://en.cppreference.com/w/cpp/language/zero_initialization. There, it is specified what happens in each of the cases you are interested in. – Daniel Langr Dec 07 '21 at 11:58
  • @DanielLangr okay I will check – Abhishek Mane Dec 07 '21 at 12:00

0 Answers0