为了解决Project Euler问题45(https://projecteuler.net/problem=45),我用c语言编写了代码。我不断收到分段错误错误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;
}
答案 0 :(得分:5)
堆栈的内存很大。代替这个
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;
答案 1 :(得分:2)
您在堆栈中有非常大的数组定义为局部变量。因此,您将获得堆栈溢出。数组pentagon
hexagon
triangle
非常大。
这些需要移动到全局空间,或者应该动态分配。对于您的用例,将数组移到全局更容易。
unsigned long pentagon[28000] = {0};
unsigned long hexagon[100000] = {0};
unsigned long triangle[500000] = {0};
int main(void)
{
int sizeofpentagon = 28000;
int sizeofhexagon = 100000;
int sizeoftriangle = 500000;
....
答案 2 :(得分:0)
自动变量的最大大小取决于实现细节。但是主要的实现可以选择设置它。
例如,如果您使用gcc或clang,自动变量将存储在堆栈中,并且堆栈大小在链接时由选项--stack <size>
控制。默认大小为2Mb,您的阵列需要628000个无符号长,因此至少为5Mb。
如果您在此代码的其他地方有更多的 standard 要求,我会尝试使用8Mb堆栈:
cc myprog.c -Wl,--stack -Wl,0x800000 -o myprog
({-Wl,
用于将参数传递到构建的链接器阶段)。
这避免了重新格式化代码(以便使用分配的数组进行演绎)仅解决编译问题。