由于某种原因,我在具有已分配初始化的变量上得到了意外的垃圾值。

#include <curses.h>
#include <sys/time.h>
#include <time.h>
#include "fmttime.h"                    // Include for formattime interface
#include <strstream>
#include <iostream>
#include <iomanip>

static int monthindex;                  // Lookup table index
static const int milli = 1000;          // Constant value for ms conversion
static const int epochyear = 1970;      // Epoch year 1970
static const int leapy = 4;             // Number of years for a leapyear
using namespace std;

struct Monthpairs                       // Fields for month & day lookup table
{
    const char* mon;                    // Months
    int day;                            // Days
};

Monthpairs months[] =                   // Lookup table for 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 which will contain the 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 the month local time
    int et_mon;         // Month of the year local time
    int et_year;        // Our current year (2013 present)
};

// Function prototype for Expanded time function
ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime);

// Expanded time will take the Epoch time and convert into days,
// months years, hours, minutes etc and then store this back into
// the Expanded Structure

ExpandedTime* localTime(
                        struct timeval* tv,     // Pointer to timeval struct
                        ExpandedTime* etime     // '' '' to expandedtime strct
                        )
{
    tzset();                                    // Corrects for timezone

    int epochT = (tv->tv_sec) - timezone;       // Epoch seconds with
    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 the milliseconds
    etime->et_sec = epochT % 60;
    epochT /= 60;                               // Turn into minutes

    etime->et_min = epochT % 60;
    epochT /= 60;                               // Turn into hours

    if (localtime(&tv->tv_sec)->tm_isdst !=0)
        etime->et_hour = (epochT % 24) + 1;     // Hours with DST correction
    else
        etime->et_hour = (epochT % 24);

    edays = tv->tv_sec / 86400;                 // Turn into days since epoch

    fouryears = edays / days4years;
    remaindays = edays % days4years;            // Determines which 4 year perio
    tempdays = remaindays;

    for (monthindex = 0 ; tempdays >= 0 ; ++monthindex)
    {
        daysgoneby += months[monthindex].day;   // Keeps track of days passed

        tempdays -= months[monthindex].day;

        if (monthindex >= 11)                   // Resets counter for 12 months
        {
            monthindex = 0;
        }
        ++monthtracker;
    }

    etime->et_day = monthindex; // (tempdays + months[monthindex].day);

    // This will take the number of months passed in current 4 year period
    // and add it to the epoch year of 1970 to find the current year

    etime->et_year = (monthtracker / 12)  + (fouryears * 4) + epochyear;

    return etime;
}

// Formats epoch time passed from a main function to a
// human readable string
char* formatTime(
                 struct timeval* tv,     // Pointer to timeval struct
                 char* buf,              // Pointer to char buf
                 size_t len              // size of buffer
                 )
{
    struct ExpandedTime etime2;         // Struct object declaration

    if (len > 0)                        // Will only print to valid sized buffer
    {
        localTime(tv, &etime2);
        ostrstream oss(buf, len);

        // Prints the time and date into a human readable string and
        // places it in the supplied buffer from 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 the length of the buffer is too small cannot write" << en
    }

    return buf;
}

例如,基本上tempday评估为某个较大的负值-185338583。 即使将其赋值为1212的初始值,也没有理由使临时性因素处于此数量级区域。 我假设我必须对程序的内存分配做了什么? 我无法弄清楚这一点。

我会说,最初我有这段代码,然后将for循环更改为while循环。 然后,我将其更改回完全相同的for循环,那就是当tempday开始起作用时。 我的假设是在tempdays之前正在输出某种垃圾值。...我不知道为什么。

  ask by Ryan translate from so

本文未有回复,本站智能推荐: