小编use*_*708的帖子

C中的分段错误(与数组相关)

我在c中编写了一个代码来解决Project Euler Problem 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++)
{ …
Run Code Online (Sandbox Code Playgroud)

c arrays segmentation-fault

3
推荐指数
1
解决办法
88
查看次数

标签 统计

arrays ×1

c ×1

segmentation-fault ×1