首页 文章

使用visual C 2008时如何解决未处理的异常错误?

提问于
浏览
2

有人可以帮我解决使用visual C 2008时未处理的异常错误吗?错误显示如下:0x00411690 in time.exe中的未处理异常:0xC0000005:访问冲突读取位置0x00000008

一些细节:

  • tm 0x00000000 Tm值 *
    tm_sec CXX0030:错误:无法计算表达式
    ...

实际上,当我过去使用visual c 6时,没有任何错误,程序运行正常 . 但是现在我使用visual 2008,我得到了这个Unhandled异常错误 .

这是程序:

...
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
  FILETIME ft;
  unsigned __int64 tmpres = 0;
  static int tzflag = 0;

  if (NULL != tv)
  {
    GetSystemTimeAsFileTime(&ft);

    tmpres |= ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    tmpres /= 10;  /*convert into microseconds*/
    /*converting file time to unix epoch*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tv->tv_sec = (long)(tmpres / 1000000UL);
    tv->tv_usec = (long)(tmpres % 1000000UL);
  }

  if (NULL != tz)
  {
    if (!tzflag)
    {
      _tzset();
      tzflag++;
    }
    tz->tz_minuteswest = _timezone / 60;
    tz->tz_dsttime = _daylight;
  }

  return 0;
}


uint32_t stampstart()
{
 struct timeval  tv;
 struct timezone tz;
 struct tm      *tm;
 uint32_t         start;

 gettimeofday(&tv, &tz);
 tm = localtime(&tv.tv_sec);

 printf("TIMESTAMP-START\t  %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour,
        tm->tm_min, tm->tm_sec, tv.tv_usec,
        tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
        tm->tm_sec * 1000 + tv.tv_usec / 1000);   /////---debugger stops here---

 start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
  tm->tm_sec * 1000 + tv.tv_usec / 1000;

 return (start);

}

谢谢你的回复:

6 回答

  • 1

    试试像......

    tm = localtime(&tv.tv_sec);
    if(tm)
    {
     printf("TIMESTAMP-START\t  %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour,
            tm->tm_min, tm->tm_sec, tv.tv_usec,
            tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
            tm->tm_sec * 1000 + tv.tv_usec / 1000); 
    
     start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
      tm->tm_sec * 1000 + tv.tv_usec / 1000;
    }
    else
    {
     // failed to retrive local time
    }
    
  • 1

    访问冲突是指地址0x00000008,这可能意味着您的代码正在访问一个结构中偏移量为8的字段,并使用NULL指针指向该字段 . 我猜本地时间返回一个NULL指针 . 检查一下 .

  • 2

    “访问冲突读取位置0x00000008”

    低可能由访问空指针引起的地址 . 在这种情况下,由于 localtimegettimeofday 的问题, tm 可能为0 . 您获取地址0x08而不是0x00,因为编译器正在尝试将8个字节的值读入结构中 .

  • 1

    成员 tv.tv_sec 的类型是 long ,但 localtime 需要 time_t * 参数 . 在VC6中这是有效的,因为 longtime_t 都是32位,但在VS2008中 time_t 是64位类型,因此它们是不兼容的 .

    这应该解决它:

    //add to beginning of stampstart function:
    time_t t;
    
    //... other code...
    
    //put this instead of call to localtime:
    t = tv.tv_sec;
    tm = localtime(&t);
    
  • 0

    使用该地址,我的SWAG是tm为NULL . 由于在调用localtime()之后永远不会检查NULL,因此您将取消引用NULL指针 .

    编辑:SWAG =科学野驴猜猜 .

    地址0x00000008与tm.tm_hour的偏移量相同 . 当您将参数传递给printf()时,您正尝试访问tm-> tm_hour .

  • 0

    为了捕获这种异常,需要对一个小项目进行调整 . 只需在项目设置中启用/ EHa选项 . 见 Project Properties -> C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions" . 而已!

    详情请见:http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.80).aspx

相关问题