1

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

#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,也没有理由将 tempdays 置于这个数量级范围内。我假设我必须对程序的内存分配做了什么?这是我无法弄清楚的事情。

我会说最初我有这段代码,然后将 for 循环更改为 while 循环。然后我将它改回完全相同的 for 循环,这就是 tempdays 开始变得奇怪的时候。我的假设是 tempdays 正在输出某种垃圾值……我不知道为什么。

4

0 回答 0