How are local and global variables initialized by default?

后端 未结 10 1669
粉色の甜心
粉色の甜心 2020-11-30 10:39

Based on below, am i right?

  • global_A reference is initialized to null.
  • global_int is 0
  • local_A reference is null
  • local_int is uninit
相关标签:
10条回答
  • 2020-11-30 10:52

    They all require to be initialized. The compiler will give you a warning about this.

    0 讨论(0)
  • 2020-11-30 10:53
    A global_A;
    

    This is an instace, not a pointer, your program will call the constructor before entering main.

    To get a pointer to an instance and not an instance you have to write:

    A* global_A;
    

    global_int is initialized to 0, as all global variables are initialized to their defaults.

    The variable A local_A will be initialized every time your program enters the function in which it is declared by a call to its constructor.

    As before if you want a pointer to A you have to write A *local_A, but this time you have to initialize it to NULL yourself.

    A *local_A = NULL;
    

    The varialle local_int wont be initialized as it is a primitive type.

    If local_A.x is initialized depends on the constructor of A, the default constructor will not initialize local_A.x. If x where a class instance creating an instance of A will initialize x with the constructor of its class.

    0 讨论(0)
  • 2020-11-30 10:55

    global_A and local_A are not references; they are objects and created using their default constructors. Default constructor has not been specified so it will be generated, which will do nothing so the member variable will remain uninitialized.

    0 讨论(0)
  • 2020-11-30 10:58

    Basically, whenever you declare a variable, the compiler will call its default constructor unless you specify otherwise.

    The language level types (e.g. pointers, 'int', 'float', 'bool', etc) "default constructor" does absolutely nothing, it just leaves the memory as it is when it is declared (global/static variables are special cases, refer to chubsdad's answer for more on the specifics). This means that they can be pretty much anything because you usually can't be sure what was in that memory previously or even where the memory came from (except in the case of the 'placement new' operator).

    The class you created has no constructors so the compiler will generate a default constructor for you which simply calls the constructor of each of its members/variables. If you incorporate the information provided in the previous paragraph, you can see that the variable 'x' will have its default constructor called, which does nothing, and thus is isn't initialized to any value.

    As others have said, there are no references in your code or pointers, so the term 'NULL' is invalid in all cases here. NULL usually refers to a pointer which, like other language level types, doesn't get set to anything until you assign it a value (unless of course its a global/static variable).

    0 讨论(0)
  • 2020-11-30 11:02

    There are no references in your code, so any of your points that mention "references" make no sense.

    In your example, both global object - global_int and global_A - are zero-initialized. Both local objects - local_int and local_A - contain indeterminate values, which means that local_int and local_A.x are not initialized.

    P.S. Of course, as other already noted, your code is non-compilable. You can't declare A objects before declaring class A (and you are missing a ; after the class definition).

    0 讨论(0)
  • 2020-11-30 11:03

    global_A reference is initialized to null.

    No, its a valid object (constructed based on default constructor, which you don't have in your code but compiler adds that)

    global_int is 0

    yes

    local_A reference is null

    no, same reason as for global

    local_int is uninitialized

    no, its initialized to 0

    Both global_A.x and local_A.x is uninitialized.

    no both are initialized to 0

    0 讨论(0)
提交回复
热议问题