调用GetSystemTime()函数时出现分段故障(Windows)
Segmentation Fault upon call to GetSystemTime() function (Windows)
我在处理分段故障时遇到问题。以下健全性检查抛出一个:
#include <windows.h>
int main()
{
LPSYSTEMTIME startTime;
GetSystemTime(startTime);
}
然而,我在一个更大的程序中使用了相同的代码,它运行起来没有问题。我曾尝试在不同的地方添加延迟,以防问题源于GetSystemTime()
中的某种多线程,但它并没有解决问题。
我正在CodeBlocks中使用MinGW GCC进行编译。
LPSYSTEMTIME
是指向SYSTEMTIME
结构的指针。你没有初始化指针,所以它指向风景中的某个地方,因此出现了访问违规/seg错误。
为了使代码正常工作,您必须确保指针指向有效的SYSTEMTIME
结构。根据MSDN文档,Microsoft明确警告您,为指针传递NULL将导致访问冲突。
您使用的是未初始化的指针。这样效果更好:
#include <windows.h>
int main()
{
SYSTEMTIME startTime;
GetSystemTime(&startTime);
}
LPSYSTEMTIME是一个指针,您需要分配内存,或者需要释放LPSYSTEMTIME中的LP并调用函数,如:
SYSTEMTIME startTime;
GetSystemTime(&startTime);
LPSYSTEMTIME
是指向SYSTEMTIME
的指针,但尚未初始化。
试试这个:
SYSTEMTIME startTime = { 0 };
GetSystemTime(&startTime);
对于使用LPSYSTEMTIME,您应该编写以下内容:
SYSTEMTIME systemTime; //allocation of memory
printf("%pn", &systemTime.wDay); // we can see address of wDay for example.
LPSYSTEMTIME st = &systemTime;
GetSystemTime(st);
printf("%d n", st->wDay); //display result
printf("%d n", (*st).wDay); //display the same result
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 分段故障背包问题
- 分段故障 11,从类函数显示动态 C 字符串
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- 在C++中,当指向删除和指向不同对象时,分段故障指针
- 分段故障说明
- 分段故障(核心转储)-不知道为什么
- 分段故障线程
- hiredis SET遇到分段故障
- 分段故障,合并排序算法
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 到达主C++之前分段故障
- 分段故障核心使用 IF流转储
- 使用向量的移动键盘排列(分段故障)
- 在二进制树插入和遍历期间,我得到了分段故障
- 分段故障在类之间返回整数
- C++分段故障BST