1

See my code below:

class A
{
 public:

  A()
  {
    i = 0;

    if(pt != NULL)
    {
      std::cout << "why" << std::endl;
    }
  }

  A(bool flag)
  {
    i = 0;
    pt = new B(3.14);
  }

 private:

  class B
  {

   public:
    B(double in) : j(in) {}

   private:
    double j;
  };

 private:

  int i;
  B *pt; 
};

int main(int argc, char *argv[])
{
  A obj; // place1

  int *p;

  if(p != NULL)
  {
    std::cout << "test2" << std::endl;
  }

  return 0;
}

In this piece of code, I wonder to know if pt will be initialized @place1.

The other question is that if I delete the definition of obj, test2 will print, if not, then why?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
xunzhang
  • 2,838
  • 6
  • 27
  • 44
  • 1
    As is discussed [here][1], pointers are not initialized on declaration. [1]: http://stackoverflow.com/questions/1910832/why-arent-pointers-initialized-with-null-by-default – Codor Apr 15 '14 at 06:32
  • Static and global variables are init to 0. Automatic variables contain garbage and may contain anything at all. – Mohit Jain Apr 15 '14 at 06:32
  • possible duplicate of [What happens to a declared, uninitialized variable in C? Does it have a value?](http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – Ed S. Apr 15 '14 at 06:57

6 Answers6

1

Value of p is undefined. It may be 0 or 1 or 2 or ...... So if it is your lucky day, you may get a print otherwise not.

Moral: Don't rely on what you get, initialize variables by yourself.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

In this piece of code, I wonder to know if pt will be initialized @place1.

It is not initialized to anything. Technically, it is default-initialized, which for a pointer means no initialization is performed. This means it can hold any value, and it is undefined behaviour to read from it.

In C++11, if you hav value-initialized obj, then pt would be zero-initialized:

A obj{}; // obj.pt is nullptr

The other question is that if I delete the definition of obj, test2 will print, otherwise not, why?

p has an undetermined value too. It can be NULL, but it may not be. Reading from it is undefined behaviour too. So, it can take the value of NULL sometimes, sometimes not, and you cannot rely on and predictable behaviour.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • why does the compiler not init the pointer as NULL by default? – xunzhang Apr 15 '14 at 06:47
  • @xunzhang It is one of those "you get what you pay for" things. C++ follows C in allowing you to decide when built-in types (in some contexts) are zero-initialized, because you don't always want to zero-initialize them, and doing so is not for free. – juanchopanza Apr 15 '14 at 06:49
  • I do not get ur true meaning. The compiler init every pointer as NULL and if you want to use it, init the pointer yourself. What's the problem in this waY? – xunzhang Apr 15 '14 at 06:53
  • @xunzhang It costs to initialize stuff. It is not for free. CPU cycles have to be spent. Sometimes you don't need to spend those cycles. C++ allows you not to do so. – juanchopanza Apr 15 '14 at 06:55
1

According to the C++ Standard local variables are not initilized implicitly. So in this code snippet

int main(int argc, char *argv[])
{
  // ...
  int *p;
  // ... 

p is not initialized by the compiler implicitly because p is a local variable. It could be zero-initialized if p had static storage duration. For example

 int *p;

int main(int argc, char *argv[])
{
  // ... 

In this code snippet variable p will be zero initialized.

As for the class A then in this code snippet

int main(int argc, char *argv[])
{
  A obj; // place1

object obj will be default initialized that is the default constructor of the class will be called. As you do not initialize data member pt in the constructor its value will be undefined.

Again if the object had static storage duration then at first the memory occupied by the object would be zero-initialized before calling the default constructor. In this case pt would be zero-initialized

A obj;

int main(int argc, char *argv[])
{
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Pointers are not initialized by default. You need to initialize it in your constructor (for members) or at the place of declaration (for local variables)

galinette
  • 8,896
  • 2
  • 36
  • 87
0

C++ will not initialize your instance variables for you. As long as you don't set them to a value, they will have a random value (or some value that looks like random garbage).

nvoigt
  • 75,013
  • 26
  • 93
  • 142
-1
if(pt != NULL)

will be probably true since pt is not initialized to NULL in code it can be any value. pointer should be initialized to NULL in code by the programmer.
you can read this related question. to know about it.

Community
  • 1
  • 1
LearningC
  • 3,182
  • 1
  • 12
  • 19