The code I wrote worked without any problem in OS X, but I got the following error when I ran it on VS 2013 community.
我写的代码在OS X中没有任何问题,但是当我在VS 2013社区上运行它时出现了以下错误。
Unhandled exception at 0x001A3D22 in Myproject.exe: 0xC0000005:
Access violation writing location 0x00000000.
Here is the code with problem. The original code has some more lines, but the code below generates the same error by itself.
这是有问题的代码。原始代码有更多行,但下面的代码本身会生成相同的错误。
#include <stdio.h>
int main(void){
int **p;
p = (int **)malloc(sizeof(int *) * 5000);
for (int i = 0; i < 5000; i++)
p[i] = (int *)malloc(sizeof(int) * 5000 * 25);
for (int i = 0; i < 5000 * 25; i++) p[0][i] = 0;
for (int i = 1; i < 5000; i++) p[i][0] = 0; //<- error on this line
printf("donw!\n");
system("pause");
return 0;
}
This is the status when error occured. i and p are colored as red.
这是发生错误时的状态。我和p的颜色为红色。
name value type
i 4118 int
p 0x00758fe8 {0x0075de48 {0}} int * *
p[0] 0x0075de48 {0} int *
p[0][i] 0 int
p[i] 0x00000000 {???} int *
Can anyone tell me what makes this kine of difference between OSX and Windows? In OSX I compiled the code with gcc.
任何人都可以告诉我是什么让OSX和Windows之间产生了这种差异?在OSX中,我用gcc编译了代码。
2
When p[i]
is NULL
, it is most likely the result of malloc
failing to allocate the needed memory. Add checks to detect that.
当p [i]为NULL时,很可能是malloc无法分配所需内存的结果。添加检查以检测它。
p = (int **)malloc(sizeof(int *) * 5000);
if ( p == NULL )
{
// Deal with error.
}
for (int i = 0; i < 5000; i++)
{
p[i] = (int *)malloc(sizeof(int) * 5000 * 25);
if ( p[i] == NULL )
{
// Deal with error
}
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/05/01/7db0c7d05ea16646b6fac2083c706bed.html。