title: Unexpected garbage value
body: <p>for some reason I am getting unexpected garbage values on a variable that has an assigned initialization. </p>
  
  <pre><code>#include &lt;curses.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;time.h&gt;
  #include "fmttime.h"                    // Include for formattime interface
  #include &lt;strstream&gt;
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  
  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 &amp; day lookup table
  {
      const char* mon;                    // Months
      int day;                            // Days
  };
  
  Monthpairs months[] =                   // Lookup table for months &amp; 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-&gt;tv_sec) - timezone;       // Epoch seconds with
      int epochUT = tv-&gt;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-&gt;et_usec = (epochUT/milli) % milli;   // Find the milliseconds
      etime-&gt;et_sec = epochT % 60;
      epochT /= 60;                               // Turn into minutes
  
      etime-&gt;et_min = epochT % 60;
      epochT /= 60;                               // Turn into hours
  
      if (localtime(&amp;tv-&gt;tv_sec)-&gt;tm_isdst !=0)
          etime-&gt;et_hour = (epochT % 24) + 1;     // Hours with DST correction
      else
          etime-&gt;et_hour = (epochT % 24);
  
      edays = tv-&gt;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 &gt;= 0 ; ++monthindex)
      {
          daysgoneby += months[monthindex].day;   // Keeps track of days passed
  
          tempdays -= months[monthindex].day;
  
          if (monthindex &gt;= 11)                   // Resets counter for 12 months
          {
              monthindex = 0;
          }
          ++monthtracker;
      }
  
      etime-&gt;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-&gt;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 &gt; 0)                        // Will only print to valid sized buffer
      {
          localTime(tv, &amp;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 &lt;&lt; etime2.et_year &lt;&lt; " " &lt;&lt;  months[monthindex].mon &lt;&lt; " " &lt;&lt;
          etime2.et_day &lt;&lt; " " &lt;&lt; setw(2) &lt;&lt; etime2.et_hour &lt;&lt; ":"
          &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; etime2.et_min &lt;&lt; ":" &lt;&lt; setfill('0')
          &lt;&lt; setw(2) &lt;&lt; etime2.et_sec &lt;&lt; ":" &lt;&lt; setfill('0') &lt;&lt; setw(3) &lt;&lt;
           etime2.et_usec &lt;&lt; ends;
      }
  
      else if (len &lt;= 0)
      {
          cout &lt;&lt; "Sorry the length of the buffer is too small cannot write" &lt;&lt; en
      }
  
      return buf;
  }
  </code></pre>
  
  <p>Basically tempday is evaluating to some large negative value -185338583 for example. Even though its assigned to an initial value of 1212, theres no reason for tempdays to even be in this area of magnitude. Im assuming i must have done something to the programs allocation of memory? This is something that I cannot figure out. </p>
  
  <p>I will say that initially I had this code, then changed the for loop to a while loop. I then changed it back to the exact same for loop and thats when tempdays started to act odd. My assumption is some sort of garbage value is being output by tempdays....i have no clue why.</p>