如何修复分段错误 11 错误? [英] How to fix segmentation fault 11 error?
问题描述
我希望主要返回dati"中mdl"出现的位置.我设置了模式"函数来查找每次出现的起点,但是当我从命令行运行程序时,它会返回:
I want the main returns the position of the occurrences of "mdl" in "dati". I set up the "schema" function to find the starting point of each occurrence, but when i run the program from the command line, it returns:
Segmentation fault: 11
我不知道如何解决这个问题.代码如下:
I don't know how to fix the problem. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int schema(int testo[], int nT, int modello[], int nM, int primo) {
int i, j, k;
static int r[12];
j=0;
for(i=primo; i<nT; i++) {
if(testo[i] == modello[0] && testo[i+1] == modello[1] && testo[i+2] == modello[2] && testo[i+3] == modello[3] && testo[i+4] == modello[4] && testo[i+5] == modello[5] && testo[i+6] == modello[6] && testo[i+7] == modello[7]) {
r[j] = i+1;
j++;
}
}
return *r;
}
int main(int argc, char** argv) {
FILE *in;
FILE *out;
int i, m;
const int n = 100;
int dati[n];
int *soluzione;
int start;
if ((in=fopen("dati.txt", "r"))==NULL){
return -1;
}
for(i=0; i<n; i++) {
if (fscanf(in, "%d", &dati[i]) < 0){
fclose(in);
return i;
}
}
int mdl[] = {0,0,0,1,1,1,0,1};
m = sizeof(mdl)/sizeof(mdl[0]);
*soluzione = schema(dati, n, mdl, m, start);
for(i=0; i<12; i++) {
printf("- risultato[%d] = %d
", i, soluzione[i]);
}
//out = fopen("risultati.txt", "w");
//...
fclose(in);
return 1;
}
我必须使用函数查找出现,我不能使用其他方式.
I have to use the function to find the occurrences, I cannot use other ways.
推荐答案
您正在取消引用指针 soluzione
,但它从未使用值初始化:
You're dereferencing the pointer soluzione
, but it was never initialized with a value:
int *soluzione;
...
*soluzione = schema(dati, n, mdl, m, start);
读取未初始化的值以及随后取消引用该未初始化的值会调用未定义行为.在这种情况下,它表现为分段错误.
Reading an uninitialized value, as well as subsequently dereferencing that uninitialized value, invokes undefined behavior. In this case, it manifests in a segmentation fault.
在这种情况下,您不需要指针.只需将变量声明为 int
.
You don't need a pointer in this case. Just declare the variable as an int
.
int soluzione;
...
soluzione = schema(dati, n, mdl, m, start);
你也不初始化start
.结果,您在可能超出数组边界的未知位置对 testo
进行索引.这也会调用未定义的行为.
You also don't initialize start
. As a result, you index into testo
at an unknown location which could be outside the bounds of the array. This also invokes undefined behavior.
看起来您实际上从 schema
返回了错误的数据类型.如果你想返回一个指向本地数组 r
的指针(在这种情况下很好,因为它被声明为 static
,函数需要返回一个 int *
你应该 return r
.
It looks like you're actually returning the wrong datatype from schema
. If you want to return a pointer to the local array r
(which in this case is fine since it's declared as static
, the function needs to return an int *
and you should return r
.
然后在 main
中,您将 soluzione
保留为指针,但直接分配给它.
Then in main
you would keep soluzione
as a pointer but assign to it directly.
int *schema(int testo[], int nT, int modello[], int nM, int primo) {
...
return r;
}
int main(int argc, char** argv) {
...
int *soluzione;
...
soluzione = schema(dati, n, mdl, m, start);
这篇关于如何修复分段错误 11 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!