简体   繁体   English

尝试填充动态数组时发生访问冲突(大量项目)

[英]Access violation on try to fill dynamic array (large number of items)

I have the following C code: 我有以下C代码:

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

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

While dimension is ~6-8 millions it works fine, but when it about 300 millions it fails with access violation. 虽然dimension约为6-8百万,它可以正常工作,但是当其为3亿时,它会因访问冲突而失败。 The following message in debug: 调试中的以下消息:

Unhandled exception at 0x012f1077 in mathpro.exe: 0xC0000005: Access violation writing location 0x00000000. mathpro.exe中0x012f1077处未处理的异常:0xC0000005:访问冲突写入位置0x00000000。

The same situation if I use memset() instead of cycle. 如果我使用memset()而不是cycle,则情况相同。

Are there any ideas how it's resolve this problem? 有什么想法可以解决这个问题吗?

"Access violation writing location 0x00000000" is explained by the manual 手册解释了“访问冲突写入位置0x00000000”

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

On error, these functions return NULL. 错误时,这些函数将返回NULL。

Or if you prefer http://www.cplusplus.com/reference/cstdlib/malloc/ . 或者,如果您更喜欢http://www.cplusplus.com/reference/cstdlib/malloc/

Return Value 返回值

On success, a pointer to the memory block allocated by the function. 成功时,指向函数分配的内存块的指针。 The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable. 该指针的类型始终为void *,可以将其强制转换为所需的数据指针类型,以便将其取消引用。 If the function failed to allocate the requested block of memory, a null pointer is returned. 如果函数未能分配所请求的内存块,则返回空指针。

You have encountered an error. 您遇到了错误。 Quite likely out of memory. 很可能内存不足。

If you were to inspect the value sizeof(double)*dimension prior to passing it to malloc, you'll find that it is indeed quite a large number. 如果要在将值sizeof(double)*dimension传递给malloc之前进行检查,您会发现它确实是一个很大的数字。

As already explained by Mr. Captain Giraffe in the answer that you're facing the issue because the memory allocation through malloc() has failed (quite likely for very large allocation request) and you did not check for the success of malloc() before using the returned pointer. 正如Giraffe上尉先生在回答中已经解释的那样,您正面临问题,因为通过malloc()的内存分配失败了(很可能是非常大的分配请求),并且您之前没有检查malloc()是否成功使用返回的指针。

In case of failure. 万一发生故障。 malloc() will return NULL and without a check, you'll be dereferencing a NULL pointer, which in turn invokes undefined behaviour . malloc()将返回NULL并且不进行检查,您将取消引用NULL指针,而NULL指针又调用未定义的行为

Along with that, I would like to add three more points, eg, 除此之外,我想补充三点,例如,

  1. Please do not cast the return value of malloc() and family in C . 不要Cmalloc()和family的返回值。

  2. You need not use a loop to initialize the (properly) allocated memory to 0 . 您无需使用loop将(正确地)分配的内存初始化为0 Rather, to get this done in an efficient manner, use either 而是要有效地完成此操作,请使用

    • you can use calloc() , which returns the memory set to zero. 您可以使用calloc() ,将内存设置为零。
    • you can use malloc() and then memset() to set the allocated memory to the particular value supplied with memset() . 您可以使用malloc()然后使用memset()将分配的内存设置为memset()提供的特定值。
  3. The robust way to use malloc() in an expression is to write the expression as 在表达式中使用malloc()可靠方法是将表达式写为

     AtS=malloc(dimension * sizeof*AtS); //sizeof is an operator 

    which is independent to the type of AtS . 这与AtS类型 AtS At a later point, if the type of AtS is changed, you don't need to modify this expression at all. 稍后,如果更改了AtS的类型,则根本不需要修改此表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM