c++ – Uninitialised value was created by a stack allocation

c++ – Uninitialised value was created by a stack allocation

The meaning of the error is essentially that youre using a variable before you assign to it. The only variables this can possibly apply to are dd, mm, yy.

This means that your sscanf call is not writing to all three of them. This will occur if you pass in a date that isnt completely specified.

Note that sscanf returns a value to tell you how many of the variables it wrote to. You should be checking the return value, and aborting (or filling in some default values) if it doesnt return 3, because then not all your fields will have been filled.

Theres no error checking for sscanf and that means some variables might remain uninitialized and afterwards used, e.g.

std::string str = invalid;
unsigned int dd,mm,yy;
cout << dd <<   << mm <<   << yy << endl;
cout << Arguments read:  << sscanf(str.c_str(),%2d %2d %4d,&mm,&dd,&yy) << endl;
cout << dd <<   << mm <<   << yy;

the above code might emit as output:

32550 3249645428 32550
Arguments read: 0
32550 3249645428 32550

where all three arguments remain uninitialized.

c++ – Uninitialised value was created by a stack allocation

Leave a Reply

Your email address will not be published.