gpt4 book ai didi

c - 使用二维数组调用函数时出现段错误

转载 作者:行者123 更新时间:2023-11-30 14:20:16 25 4
gpt4 key购买 nike

我有以下代码:

#define MAXSAMPLES 1024
typedef int sample_t;
typedef sample_t sub_band_t[MAXSAMPLES][MAXSAMPLES];

void blah(sample_t a[][MAXSAMPLES], int u0, int v0, int u1, int v1) {
. . . .
}


int main(int argc, char *argv[]) {
sub_band_t in_data;
int k =0;

if (argc < 2) {
printf("\nInput filename required\n");
return 0;
}

FILE *input_file = fopen(argv[1], "r");
char del = '\0';

int i = 0, j = 0;
int cols = 0;
sample_t x;
while (! feof(input_file)) {
if (fscanf(input_file, "%d%c", &x, &del) != 2) {
i--;
break;
}
in_data[i][j] = x;
if ( del == '\n') {
i++;
j =0;
continue;
}
j++;
cols = j > cols ? j : cols;
x = 0;
}
blah(in_data, 0, 0, i, cols);
}

当我使用包含 10*10 整数的输入文件运行此程序时,我在 main 中的 blah 函数调用处遇到段错误。我也无法使用 gdb 收集有关段错误的任何信息,它只是说:

0x0000000000400928 in blah (a=Cannot access memory at address 0x7ffffdbfe198) at blah.c

我在这里做错了什么?任何帮助将不胜感激。

最佳答案

您将 subband_t 输入为一个几 MB 的大型二维数组。这将需要几 MB 的堆栈内存。这是否有效取决于实现的质量。程序是否存在 #define MAXSAMPLES 10 段错误?那么这就是你的问题了。

请注意

 while (! feof(input_file)) { ... }

从来没有工作过,也永远不会工作,因为 EOF 标志仅在输入操作命中 EOF 之后设置。请参阅 comp.lang.c 常见问题解答。

关于c - 使用二维数组调用函数时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612828/

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