gpt4 book ai didi

c - C 中的段错误(数组相关)

转载 作者:太空狗 更新时间:2023-10-29 16:00:23 25 4
gpt4 key购买 nike

我用 c 语言编写了一段代码以解决欧拉计划问题 45 (https://projecteuler.net/problem=45)。我不断收到段错误 139。我确信这与尝试访问我没有权限的内存位置无关。

我的猜测是,问题与我的数组大小有关。我查了一下答案,它是大约 10 位数字。要获得十位数字,数组“三角形”的大小必须在一百万到两百万之间。但是当我把数组做得那么大时,我得到了错误。我没有在下面的代码中得到错误,因为该数组的大小是 500 000(当然这还不够)。

我使用 ubuntu 16.04 和 Geany。

如果您需要更多信息,请询问。提前致谢。

#include <stdio.h>
#include <stdlib.h>

unsigned long pentagonalgenerator(int n);
unsigned long trianglegenerator(int n);
unsigned long hexagonalgenerator(int n);

_Bool search_function(unsigned int to_be_looked_for , unsigned long array[] , int sizeofarray);

int main(void)
{
unsigned long pentagon[28000] = {0};
int sizeofpentagon = 28000;

unsigned long hexagon[100000] = {0};
int sizeofhexagon = 100000;

unsigned long triangle[500000] = {0};
int sizeoftriangle = 500000;

int counter;

for(counter = 0 ; counter < sizeofpentagon ; counter++)
{
pentagon[counter] = pentagonalgenerator(counter + 2);
}

for(counter = 0 ; counter < sizeofhexagon ; counter++)
{
hexagon[counter] = hexagonalgenerator(counter + 2);
}

for(counter = 0 ; counter < sizeoftriangle ; counter++)
{
triangle[counter] = trianglegenerator(counter + 2);
}

printf("%lu \n%lu \n%lu \n", hexagon[sizeofhexagon - 1] , pentagon[sizeofpentagon - 1] , triangle[sizeoftriangle - 1]);


for(counter = 0 ; counter < sizeofhexagon ; counter++)
{
if(search_function(hexagon[counter] , pentagon , sizeofpentagon))
{
if(search_function(hexagon[counter] , triangle , sizeoftriangle) && hexagon[counter] != 40755)
{
printf("%lu", hexagon[counter]);
return 0;
}
}
}

return 1;
}

_Bool search_function(unsigned int to_be_looked_for , unsigned long array[] , int sizeofarray)
{
int left = 0, right = sizeofarray - 1 , middle = 0;

while(left <= right)
{
middle = (left + right) / 2;

if(to_be_looked_for == array[middle]) return 1;
else if(to_be_looked_for < array[middle]) right = middle - 1;
else if(to_be_looked_for > array[middle]) left = middle + 1;
}

return 0;
}

unsigned long pentagonalgenerator(int n)
{
unsigned int return_value = 0;

return_value = (n*(3*n - 1)) / 2;

return return_value;
}

unsigned long hexagonalgenerator(int n)
{
unsigned int return_value = 0;

return_value = n*(2*n - 1);

return return_value;
}

unsigned long trianglegenerator(int n)
{
unsigned int return_value = 0;

return_value = (n*(n + 1)) / 2;

return return_value;
}

最佳答案

堆栈需要大量内存。而不是这个

unsigned long pentagon[28000] = {0};
int sizeofpentagon = 28000;

unsigned long hexagon[100000] = {0};
int sizeofhexagon = 100000;

unsigned long triangle[500000] = {0};
int sizeoftriangle = 500000;

试试这个:

unsigned long *pentagon = calloc(28000*sizeof(unsigned long));
int sizeofpentagon = 28000;

unsigned long *hexagon = calloc(100000 * sizeof(unsigned long));
int sizeofhexagon = 100000;

unsigned long *triangle = calloc(500000 * sizeof(unsigned long));
int sizeoftriangle = 500000;

关于c - C 中的段错误(数组相关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52701391/

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