gpt4 book ai didi

c - 尝试填充动态数组时出现访问冲突(大量项目)

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:04 25 4
gpt4 key购买 nike

我有以下 C 代码:

int dimension; 
double *AtS;
...
AtS=(double*)malloc(sizeof(double)*dimension);

for (i=0; i<dimension; i++)
{
AtS[i]=0.0;
}

虽然 dimension 约为 6-8 百万,但它工作正常,但当它约为 3 亿时,它会因访问冲突而失败。调试中的以下消息:

Unhandled exception at 0x012f1077 in mathpro.exe: 0xC0000005: Access violation writing location 0x00000000.

如果我使用 memset() 而不是循环,情况相同。

有什么想法可以解决这个问题吗?

最佳答案

“Access violation writing location 0x00000000”手册有解释

http://man7.org/linux/man-pages/man3/malloc.3.html#RETURN_VALUE

On error, these functions return NULL.

或者如果你喜欢 http://www.cplusplus.com/reference/cstdlib/malloc/ .

Return Value

On success, a pointer to the memory block allocated bythe function. The type of this pointer is always void*, which can becast to the desired type of data pointer in order to bedereferenceable. If the function failed to allocate the requestedblock of memory, a null pointer is returned.

您遇到了一个错误。很可能内存不足。

如果您在将值 sizeof(double)*dimension 传递给 malloc 之前检查它,您会发现它确实是一个很大的数字。

关于c - 尝试填充动态数组时出现访问冲突(大量项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30849601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com