C++ { } class initialization tip

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Code:
#include <iostream>
#include <cstdlib>

class A {
protected:
    int a0;

public:
    void a(){
        std::cout << a0 << std::endl;
    }
};

int main(){
    A a = {};
    a.a();            //If a0 is not initialized, { } will set the uninitialized a0 to its default value, 0.
                    //If a0 is initialized, { } will do nothing about it.

    A b;            //If a0 is not initialized, simply constructing the class object will set a0 to garbage value, such as:
                    //0xFFFF FFFF CCCC CCCC or -858,993,460.
                    //This value means undefined unsigned long integer value, or uninitialized memory data.
                    //If a0 is initialized, then after the class object is constructed, the class member, a0,
                    //will have that initialized value set as its default.
    b.a();

    system("pause");
    return 0;
}
 

jonas

Ultra Cool Member
Btw, I'm pretty sure that your comment "A b; //If a0 is not initialized, simply constructing the class object will set a0 to garbage value" is incorrect. I think accessing b's a0 is simply undefined behavior. Your comment seems to imply that there is a non-deterministic but well-defined choice for the value, which is really not the case. In practice you may see such a non-deterministic choice because compilers are lazy and generate the "expected" code, but at the point that you take the value of b.a0 in the call of b.a() your compiler could just as well download a virus or simply crash your application (if they had a flag for each variable "initialized" and checked it before accessing the variable).
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Btw, I'm pretty sure that your comment "A b; //If a0 is not initialized, simply constructing the class object will set a0 to garbage value" is incorrect. I think accessing b's a0 is simply undefined behavior. Your comment seems to imply that there is a non-deterministic but well-defined choice for the value, which is really not the case. In practice you may see such a non-deterministic choice because compilers are lazy and generate the "expected" code, but at the point that you take the value of b.a0 in the call of b.a() your compiler could just as well download a virus or simply crash your application (if they had a flag for each variable "initialized" and checked it before accessing the variable).
I wasn't really sure about that, since it generates the same output for GCC and MSVC++. But I am not well-versed in the grittier details underneath it, so I really don't know.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    It is possible to automate it so that instead of moving to Archive if it has the Science, technology or sci tech tag that it gets archived there instead. I am considering a new Weird News Tag
  • tom_mai78101 tom_mai78101:
    Please do. I'll try to tag the news appropriately when I get the chance.
  • The Helper The Helper:
    The new Discourse forum software we are moving to next does alot with the tags and also hashtags
  • The Helper The Helper:
    OK I found the source of your burrito post stuff it is from Reddit and possibly a facebook post too but definitely reddit
  • The Helper The Helper:
    Happy Monday!
  • The Helper The Helper:
    Happy Tuesday :)
  • Blackveiled Blackveiled:
    Happy Tuesday
  • The Helper The Helper:
    Blackveiled! What is up?
  • The Helper The Helper:
    I saw XXXConan on here the other day I dont know if you remember him
  • The Helper The Helper:
    blasts from the past
  • Blackveiled Blackveiled:
    I remember him, buncha old faces
  • jonas jonas:
    He's a software engineer at a hiring platform now I think
  • The Helper The Helper:
    I wanna know if his mom is still single LOL
  • tom_mai78101 tom_mai78101:
    I need context.
    +1
  • The Helper The Helper:
    Back in the day conan was a young one and his mom was single and I used to tease him that I was going to date his mom and be his daddy - yeah sounds bad now but it was funny at the time
  • The Helper The Helper:
    his mom is my age
  • The Helper The Helper:
    i know right it was pretty funny
  • thewrongvine thewrongvine:
    its pretty funny
    +1
  • tom_mai78101 tom_mai78101:
    Thanks for the context.
    +1
  • The Helper The Helper:
    Happy Hump Day!!!
  • Blackveiled Blackveiled:
    TH old fart gotta have a beer with me one of these days
  • The Helper The Helper:
    I am down anytime Blackveiled
  • Blackveiled Blackveiled:
    count on it then cause you don't live that far from me hah
  • The Helper The Helper:
    really that is cool! I am in Houston in the 77079 area

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top