View Full Version : Check if a variable has NOT been initialized

June 14th, 2011, 03:15 AM
Is it possible to check if a variable has not been initialized in C++? Or if a variable HAS been initialized? If so how.

The type would be int and anyone with OpenCV experience what happens if:

int deviceID;
CvCapture * camera = cvCreateCameraCapture(deviceID);Where deviceID has not been initialized, would it just go to default zero?

June 14th, 2011, 03:22 AM
The deviceID is initialized to the default value of 0. You only need to worry about this with pointers.

That is why when you create them you initialize them with NULL so you can check them later.

June 14th, 2011, 03:32 AM
Well, it depends whether deviceID is local or global. If global then it would be 0 but if local it would be whatever is on the stack - random data.

You should init the variable to some value you know and can test if you need to detect if it has been initialized by other code.


int deviceID = -1;

... later

if(deviceID != -1)
camera = cvCreateCameraCapture(deviceID);

June 14th, 2011, 08:07 AM
There's a good explanation here: http://stackoverflow.com/questions/2218254/variable-initialization-in-c/2218275#2218275