0

First case:

class x
{
public:
    x(){}
    int mem;
}

Second case:

class x
{
public:
    int mem;
}

int main()
{
    x a;
    std::cout << a.mem; //member not initialized error in second case
}

If we do not define the default constructor, the compiler will add one; and the function of the constructor is initializing the memory. So why is it giving an error in the second case, but not in the first case?

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
user649558
  • 57
  • 5

2 Answers2

2

The member variable m is not being initialized in either case. You need to initialize it explicitly in the constructor

class x
{
public:
    x() : mem{} {}
    int mem;
};
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • @AdrianCornish Nope, C++11 [uniform initialization syntax](https://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization) – Praetorian Aug 23 '12 at 04:12
  • Cool - Anyone quote/give the number of the paragraph so I can go read it - did not know it changed. – Adrian Cornish Aug 23 '12 at 04:14
  • 2
    @AdrianCornish You can still use the old style of course, and I actually think the use of C++11 should have been made explicit above. In any case, the paragraph is §8.5.4/1: _[...] List-initialization can be used [...] as an initializer for a non-static data member_. – jogojapan Aug 23 '12 at 04:18
  • Also when i allocate memory using new to object than I do not need to specify the default constructor explicitly.So that concludes memory on stack is not initiazed but on heap is initialized. correct me if i am wrong. Please ignore syntactical mistakes. – user649558 Aug 23 '12 at 04:22
  • Wouldn't it be initialized in the first? I was under the impression that leaving out the initializers inserted blank ones for you, as [this example](http://ideone.com/4GbJW) would suggest, or am I just missing something like POD vs non-POD? – chris Aug 23 '12 at 04:24
  • 2
    @chris No, primitive types are not initialized unless explicitly done in the constructor. [Here's](http://liveworkspace.org/code/ef13072e44e1dff98035a254f8a4250a) an example. – Praetorian Aug 23 '12 at 04:27
  • 1
    @user649558 No, there is no such requirement that heap memory is initialized before it is handed to you by the system. However, most implementations will do that as a security measure. – Praetorian Aug 23 '12 at 04:28
  • @chris if you explicitly call :mem() in the init list then yes it will get a value of 0 for a int for example. – Adrian Cornish Aug 23 '12 at 04:29
  • @Prætorian, Ah, just primitives. Thanks. – chris Aug 23 '12 at 04:29
  • @Prætorian With heap memory there is no requirement for an OS to init it - I would never rely on it unless it was required by the language standard. VMS used to init local vars to zero - nightmare when porting – Adrian Cornish Aug 23 '12 at 04:30
  • @chris No, not just primitives, aggregates [behave the same](http://liveworkspace.org/code/4834eb0dc250e6d5313c03b005964c7d). Anything without a user defined constructor will remain uninitialized. – Praetorian Aug 23 '12 at 04:32
  • @Prætorian But C++ does fake a default constructor for primitives if you explicitly call it. – Adrian Cornish Aug 23 '12 at 04:35
  • @AdrianCornish There's no *faking*; the primitive is [value initialized](http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat). – Praetorian Aug 23 '12 at 04:37
  • @Prætorian Not well put on my part - I meant if you call m_intvar() in init list then it will have a defined value - but you ignore m_intvar then it could be anything. That is what I meant by c++ faking constructors for primitive types. – Adrian Cornish Aug 23 '12 at 04:41
1

You are missing the semicolon at the end of the class def - this is your problem. Also do not think the default constructor will init your member vars unless they are also classes which have a requirement to init in a certain way

class x
{
public:
    int mem;
};

int main()
{
    x a;
    std::cout << a.mem; //member not initialized error in second case
}
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77