gpt4 book ai didi

c - 在 C 中分配无符号长整型内存时出错

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

我正在编写一个需要使用一些非常大的数组的 C 程序。我通过调用为我的数组分配内存:

unsigned long *visited = declareArrayofLongs(my_pow(2, d));

declareArrayofLongs() 函数是:

unsigned long int* declareArrayofLongs(unsigned long int length)
{
unsigned long int *myarray = (unsigned long int*) malloc(length*sizeof(unsigned long int));
if (myarray==NULL)
printf("Error allocating memory for unsigned longs!\n");
unsigned long int i;
for(i=0; i<length; i++)
myarray[i] = 0;
return myarray;
}

my_pow()

unsigned long int my_pow(int base, int exp)
{
unsigned long int pow=1;
int i=0;
for(i=0; i<exp; i++){
pow = (unsigned long int)base*pow;
}
return pow;
}

当程序运行并达到 d=29 左右时,我得到“为无符号长整型分配内存时出错!”。我的系统上有 16gb 的 RAM。我难道不能处理这么大的分配量吗?

一次只声明一个visited 数组。在重新分配之前,我有一个 free(visited) 调用。

最佳答案

您没有显示 my_pow。既然你的代码不起作用,是什么让你认为它做了你认为它做的事?

您可能正在运行 32 位代码,在这种情况下,无论 RAM 有多大,您都不会获得超过 3GB 的空间。

关于c - 在 C 中分配无符号长整型内存时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27605593/

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