c++ - Unexpected garbage value -
for reason getting unexpected garbage values on variable has assigned initialization.
#include <curses.h> #include <sys/time.h> #include <time.h> #include "fmttime.h" // include formattime interface #include <strstream> #include <iostream> #include <iomanip> static int monthindex; // lookup table index static const int milli = 1000; // constant value ms conversion static const int epochyear = 1970; // epoch year 1970 static const int leapy = 4; // number of years leapyear using namespace std; struct monthpairs // fields month & day lookup table { const char* mon; // months int day; // days }; monthpairs months[] = // lookup table months & days { {"jan", 31}, {"feb", 28}, {"mar", 31}, {"apr", 30}, {"may", 31}, {"jun", 30}, {"jul", 31}, {"aug", 31}, {"sep", 30}, {"oct", 31}, {"nov", 30}, {"dec", 31}, }; // structure contain human readable local // time values struct expandedtime { int et_usec; // number of milliseconds local time int et_sec; // number of seconds local time int et_min; // number of minutes local time int et_hour; // number of hours local time int et_day; // day of month local time int et_mon; // month of year local time int et_year; // our current year (2013 present) }; // function prototype expanded time function expandedtime* localtime(struct timeval* tv, expandedtime* etime); // expanded time take epoch time , convert days, // months years, hours, minutes etc , store // expanded structure expandedtime* localtime( struct timeval* tv, // pointer timeval struct expandedtime* etime // '' '' expandedtime strct ) { tzset(); // corrects timezone int epocht = (tv->tv_sec) - timezone; // epoch seconds int epochut = tv->tv_usec; // epochtime microseconds int edays; // days since epochtime int days4years = 1460; // number of days in 4 years int fouryears; // number of 4 year periods int remaindays; // number of days leftover int tempdays; // holds value of remaindays int monthtracker = 0; // number of total months passed // since current 4 year period int daysgoneby = 0; // number of days passed // since current 4 year period etime->et_usec = (epochut/milli) % milli; // find milliseconds etime->et_sec = epocht % 60; epocht /= 60; // turn minutes etime->et_min = epocht % 60; epocht /= 60; // turn hours if (localtime(&tv->tv_sec)->tm_isdst !=0) etime->et_hour = (epocht % 24) + 1; // hours dst correction else etime->et_hour = (epocht % 24); edays = tv->tv_sec / 86400; // turn days since epoch fouryears = edays / days4years; remaindays = edays % days4years; // determines 4 year perio tempdays = remaindays; (monthindex = 0 ; tempdays >= 0 ; ++monthindex) { daysgoneby += months[monthindex].day; // keeps track of days passed tempdays -= months[monthindex].day; if (monthindex >= 11) // resets counter 12 months { monthindex = 0; } ++monthtracker; } etime->et_day = monthindex; // (tempdays + months[monthindex].day); // take number of months passed in current 4 year period // , add epoch year of 1970 find current year etime->et_year = (monthtracker / 12) + (fouryears * 4) + epochyear; return etime; } // formats epoch time passed main function // human readable string char* formattime( struct timeval* tv, // pointer timeval struct char* buf, // pointer char buf size_t len // size of buffer ) { struct expandedtime etime2; // struct object declaration if (len > 0) // print valid sized buffer { localtime(tv, &etime2); ostrstream oss(buf, len); // prints time , date human readable string , // places in supplied buffer plot.cc oss << etime2.et_year << " " << months[monthindex].mon << " " << etime2.et_day << " " << setw(2) << etime2.et_hour << ":" << setfill('0') << setw(2) << etime2.et_min << ":" << setfill('0') << setw(2) << etime2.et_sec << ":" << setfill('0') << setw(3) << etime2.et_usec << ends; } else if (len <= 0) { cout << "sorry length of buffer small cannot write" << en } return buf; }
basically tempday evaluating large negative value -185338583 example. though assigned initial value of 1212, theres no reason tempdays in area of magnitude. im assuming must have done programs allocation of memory? cannot figure out.
i had code, changed loop while loop. changed exact same loop , thats when tempdays started act odd. assumption sort of garbage value being output tempdays....i have no clue why.
Comments
Post a Comment