如何修复分段错误 11 错误?
How to fix segmentation fault 11 error?
我想要主要 returns "mdl" 在 "dati" 中出现的位置。我设置了 "schema" 函数来查找每次出现的起点,但是当我从命令行 运行 程序时,它 returns:
Segmentation fault: 11
我不知道如何解决这个问题。
这是代码:
#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\n", i, soluzione[i]);
}
//out = fopen("risultati.txt", "w");
//...
fclose(in);
return 1;
}
我必须使用函数来查找出现的事件,我不能使用其他方法。
您正在取消引用指针 soluzione
,但它从未用值初始化:
int *soluzione;
...
*soluzione = schema(dati, n, mdl, m, start);
读取未初始化的值,以及随后取消引用该未初始化的值,调用 undefined behavior。在这种情况下,它表现为分段错误。
在这种情况下您不需要指针。只需将变量声明为 int
.
int soluzione;
...
soluzione = schema(dati, n, mdl, m, start);
你也没有初始化start
。结果,您在可能位于数组边界之外的未知位置对 testo
进行索引。这也会调用未定义的行为。
编辑:
看起来您实际上 return 从 schema
获取了错误的数据类型。如果你想 return 指向局部数组 r
的指针(在这种情况下很好,因为它被声明为 static
,函数需要 return 一个 int *
你应该 return r
.
然后在 main
中,您将保留 soluzione
作为指针,但直接分配给它。
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);
我想错误出在以下代码段:
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]) {
请注意,您将大小为 n
的整数数组 dati
作为 testo
传递,并将 n
作为 [=15= 的值传递].因此,testo
的大小为 nT
。
但是在你的循环中,i
可能运行到 nt-1
,你访问 testo[i+7]
,这超出了 testo
的边界,对吗?
我想要主要 returns "mdl" 在 "dati" 中出现的位置。我设置了 "schema" 函数来查找每次出现的起点,但是当我从命令行 运行 程序时,它 returns:
Segmentation fault: 11
我不知道如何解决这个问题。 这是代码:
#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\n", i, soluzione[i]);
}
//out = fopen("risultati.txt", "w");
//...
fclose(in);
return 1;
}
我必须使用函数来查找出现的事件,我不能使用其他方法。
您正在取消引用指针 soluzione
,但它从未用值初始化:
int *soluzione;
...
*soluzione = schema(dati, n, mdl, m, start);
读取未初始化的值,以及随后取消引用该未初始化的值,调用 undefined behavior。在这种情况下,它表现为分段错误。
在这种情况下您不需要指针。只需将变量声明为 int
.
int soluzione;
...
soluzione = schema(dati, n, mdl, m, start);
你也没有初始化start
。结果,您在可能位于数组边界之外的未知位置对 testo
进行索引。这也会调用未定义的行为。
编辑:
看起来您实际上 return 从 schema
获取了错误的数据类型。如果你想 return 指向局部数组 r
的指针(在这种情况下很好,因为它被声明为 static
,函数需要 return 一个 int *
你应该 return r
.
然后在 main
中,您将保留 soluzione
作为指针,但直接分配给它。
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);
我想错误出在以下代码段:
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]) {
请注意,您将大小为 n
的整数数组 dati
作为 testo
传递,并将 n
作为 [=15= 的值传递].因此,testo
的大小为 nT
。
但是在你的循环中,i
可能运行到 nt-1
,你访问 testo[i+7]
,这超出了 testo
的边界,对吗?