c - 0x0F6F8EF7 (msvcr120d.dll) 0xC0000005 : Access violation reading location 0x091C5B30 处未处理的异常

标签 c

我正在尝试写入 txt 文件,但在程序写入几乎整个文本后,我收到下一个错误:

Unhandled exception at 0x0F6F8EF7 (msvcr120d.dll) 0xC0000005: Access violation reading location 0x091C5B30.

跟踪设置为:

trace = fopen("trace.txt", "w");  

arr 设置为:

int arr[18]; // (also all of the arr values initial to zero.)

for (i = 0; i < 18; i++){
    fprintf(trace, "%08x ", arr[i]);
}

最佳答案

我的猜测是,循环的迭代次数超出了预期,导致arr[i]超出了其固定大小。除此之外,问题可能出在跟踪初始化中。请提供相关信息!

这对我有用:

int i;
int mark[5] = {19, 10, 8, 17, 9};
FILE *trace = fopen("C:\\trace.txt", "w");
if (trace == NULL)
{
      printf("Error opening file!\n");
}
for(i = 0; i <= 4; i++){
    fprintf(trace,"%d ", mark[i]);

}
fclose(trace);

结果:

enter image description here

帖子更新后: 要正确迭代 arr[18],您的 for 循环应如下所示:

for(i = 0; i <= 17; i++)

这是因为当您定义具有 18 个元素的数组时,您可以使用从 arr[0] 到 arr[17] 的成员。

如果您查看上面的示例,这里是相同的代码片段结果,但是当您像这样运行 for 循环时:

for(i = 0; i <= 5; i++)     

请注意,我已将 i <= 4 更改为 i <= 5

结果:

enter image description here

正如您所看到的,我们正在访问数组边界之外的元素,导致程序在文本文件中写入垃圾。

关于c - 0x0F6F8EF7 (msvcr120d.dll) 0xC0000005 : Access violation reading location 0x091C5B30 处未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303777/

相关文章:

c - 错误 : expected expression before 'unsigned'

c++ - 生成二维魔法六边形格子的算法

c - 局部 const 变量将存储在哪里?

c - 来自 gsoap header 的 http 状态

c++ - 制作 : No targets provided near line 28 (CMake)

c - fputs() 在 MacOSX 上的行为不正常

c - 为什么这个程序在将 j 赋值给 k 后返回 10 作为 j 的值?其中 k=12

字符常量对于其类型来说太长,出了什么问题?

perror 示例可以改进吗?

c - 为什么我的指数数字要四舍五入? (C语言)