0

So I have the following code:

#include<iostream>
using namespace std;

class baseClass
{
public:
    int objID;

    /*baseClass()
    {}
    */
};


int main(int argc, char** argv)
{
    baseClass bcObj;
    cout << "bcObj.objID: " << bcObj.objID << endl;
    return 0;
}

Now if I try to run this, I get a runtime error about using uninitialized variable. But if I add a default constructor (uncomment the constructor), it works fine. It still prints out a garbage value, but it prints with no runtime errors.

Why is this difference? The default constructor is not doing anything to the objID. I'm using visual studio 2013 (language extensions disabled).

Thank you.

madu
  • 5,232
  • 14
  • 56
  • 96
  • 2
    Is the runtime error still triggering if you compile in release mode ? Also it would be helpful copying the exact text of the error. – J.N. Jan 12 '15 at 04:52
  • 1
    It's some debugging feature in visual studio only when compiled in debug – David Jan 12 '15 at 04:53
  • You are right. It doesnt happen in release more. Only debugging. Thank you very much guys. I was thinking there is some hidden thing in constructors. – madu Jan 12 '15 at 04:55

1 Answers1

0

It is undefined behaviour in both cases. Your compiler is only smart enough to detect one of the two cases.

M.M
  • 138,810
  • 21
  • 208
  • 365