An uninitialized value was created by stack allocation

I debugged my code using the Valgrind tool. It shows this error with this function. I gave below the error and the "My" function. I do not know what's the problem? How can i fix this? My mistake.

An uninitialized value was created by allocating the stack to 0x80996D7: cdtojd (std :: string const &)

My code.

double cdtojd(const string &cdate); double cdtojd(const string &cdate) { int dd,mm,yy; int y,m; double jd=0; //mm = atoi(cdate.substr(0,2).c_str()); //dd = atoi(cdate.substr(2,2).c_str()); //yy = atoi(cdate.substr(4,4).c_str()); sscanf(cdate.c_str(),"%2d%2d%4d",&mm,&dd,&yy); //cout<<mm<<"..."<<dd<<"...."<<yy<<endl; y = (yy - 1900) * 372; m = (mm-1) * 31; jd = dd + m + y; return jd; } 
+5
source share
2 answers

The meaning of the error is that you use the variable before assigning it. The only variables this may relate to are dd , mm , yy .

This means that your sscanf call is not recorded to all three of them. This will happen if you pass in a date that has not been fully indicated.

Note that sscanf returns a value to tell you how many of the variables it is written to. You should check the return value and abort (or fill in some default values) if it does not return 3, because then not all of your fields will be filled.

+3
source

There is no error for sscanf , and this means that some variables can remain uninitialized and subsequently be used, for example

 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 may be emitted as a result:

 32550 3249645428 32550 Arguments read: 0 32550 3249645428 32550 

where all three arguments remain uninitialized.

+1
source

Source: https://habr.com/ru/post/1203989/


All Articles