在功能引起的malloc和sscanf赛格故障 [英] seg fault caused by malloc and sscanf in a function

查看:128
本文介绍了在功能引起的malloc和sscanf赛格故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个文本文件(见下文),请阅读每一行的第一个int和其存储在一个数组,但我得到一个分段错误。我摆脱了所有海湾合作委员会的警告,我通过几个教程我在网上发现了阅读和搜索计算器解决办法,但我便无法做出来,什么我做错了。

它工作时,我有在主函数(见例1)的一切,而不是当我把它转移到第二个功能(见例2进一步下跌)。在例2中我得到的,当我跨preT GDB正确地 sscanf的一个赛格故障(线路,%i的,课程由[i]);

我很害怕,但也可能是微不足道的,但我已经浪费了这一天。

在此先感谢。

【例1】尽管与主一切工作:

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;const int的长度= 1024;诠释主(){
  字符*文件名=somedatafile.txt;
  INT *类;
  INT线;
  FILE * PFILE = NULL;
  焦线[长度]
  PFILE = FOPEN(文件名,R);
  INT numlines = 0;
  字符* P;  而(与fgets(行,长度,PFILE)){
    numlines ++;
  }  倒带(PFILE);  班=(INT *)malloc的(numlines *的sizeof(INT));
  如果(类== NULL){
    的printf(\\ nMemory错误。);
    出口(1);
  }
  INT I = 0;
  而(与fgets(行,长度,PFILE)){
    的printf(\\ n);
    P = strtok的(行,);
    p值= strtok的(NULL,,);
    sscanf的(行,%I,&安培;类[I]);
    我++;
  }
  FCLOSE(PFILE);
  返回1;
}

【例2】这不符合转移到一个函数的功能:

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;const int的长度= 1024;无效的read_data(INT **班,为int *线条,字符*文件名){
  FILE * PFILE = NULL;
  焦线[长度]
  PFILE = FOPEN(文件名,R);
  INT numlines = 0;
  字符* P;  而(与fgets(行,长度,PFILE)){
    numlines ++;
  }  倒带(PFILE);  *类=(INT *)malloc的(numlines *的sizeof(INT));
  如果(*类== NULL){
    的printf(\\ nMemory错误。);
    出口(1);
  }
  INT I = 0;
  而(与fgets(行,长度,PFILE)){
    的printf(\\ n);
    P = strtok的(行,);
    p值= strtok的(NULL,,);
    sscanf的(行,%I,班[I]);
    我++;
  }
  FCLOSE(PFILE);
  *线= numlines;
}诠释主(){
  字符*文件名=somedatafile.txt;
  INT *类;
  INT线;  的read_data(安培;类,&安培;行,文件名);
  的for(int i = 0; I<线;我++){
    的printf(\\ nclasses [I] =%I,班[I]);
  }
  返回1;
}

[somedatafile.txt内容]

  50 21 77 0 0 28 27 48 22 2
55 0 92 0 0 26 36 92 56 4
53 0 0 82 52 29 -5 2 30 1
37 0 0 76 28 18 40 48 8 1
37 0 0 79 34 -26 43 46 1 2
85 0 88 -4 6 1 3 83 80 5
56 0 0 81 11 -4 25 86 62 4
55 -1 95 -3 54 -4 40 41 1 2
53 8 77 0 0 28 23 48 24 4
37 0 101 28 -7 0 64 73 8 1
...


解决方案

 的sscanf(行,%I,班[I]);

可能是错误的。您需要取消引用有太多,请尝试:

 的sscanf(行,%I,及(*类)[我]);

这是因为是一个指向整数数组。你想那些整数中的一个地址,让的sscanf()可以在那里写的解析数。因此,你必须首先解除引用来获取数组,然后说,你想要的元素数量的地址 I 中该数组。

您也可以使用

 的sscanf(行,%I,*类+ I);

这可能是更清晰,取决于你是多么的惬意这些事情。

I want to open a text file (see below), read the first int in every line and store it in an array, but I get an segmentation fault. I got rid of all gcc warnings, I read through several tutorials I found on the net and searched stackoverflow for solutions, but I could't make out, what I am doing wrong.

It works when I have everything in the main function (see example 1), but not when I transfer it to second function (see example 2 further down). In example 2 I get, when I interpret gdb correctly a seg fault at sscanf (line,"%i",classes[i]);.

I'm afraid, it could be something trivial, but I already wasted one day on it.

Thanks in advance.

[Example 1] Even though that works with everything in main:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const int LENGTH = 1024;

int main() {
  char *filename="somedatafile.txt";
  int *classes;
  int lines;      
  FILE *pfile = NULL; 
  char line[LENGTH];
  pfile=fopen(filename,"r");
  int numlines=0;
  char *p;

  while(fgets(line,LENGTH,pfile)){
    numlines++;
  }

  rewind(pfile);

  classes=(int *)malloc(numlines*sizeof(int));
  if(classes == NULL){
    printf("\nMemory error.");
    exit(1);
  }
  int i=0;
  while(fgets(line,LENGTH,pfile)){
    printf("\n");
    p = strtok (line," ");
    p = strtok (NULL, ", ");
    sscanf (line,"%i",&classes[i]);
    i++;
  }
  fclose(pfile);
  return 1;
}

[Example 2] This does not with the functionality transfered to a function:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const int LENGTH = 1024;

void read_data(int **classes,int *lines, char *filename){
  FILE *pfile = NULL; 
  char line[LENGTH];
  pfile=fopen(filename,"r");
  int numlines=0;
  char *p;

  while(fgets(line,LENGTH,pfile)){
    numlines++;
  }

  rewind(pfile);

  * classes=(int *)malloc(numlines*sizeof(int));
  if(*classes == NULL){
    printf("\nMemory error.");
    exit(1);
  }
  int i=0;
  while(fgets(line,LENGTH,pfile)){
    printf("\n");
    p = strtok (line," ");
    p = strtok (NULL, ", ");
    sscanf (line,"%i",classes[i]);
    i++;
  }
  fclose(pfile);
  *lines=numlines;
}  

int main() {
  char *filename="somedatafile.txt";
  int *classes;
  int lines;

  read_data(&classes, &lines,filename) ;
  for(int i=0;i<lines;i++){
    printf("\nclasses[i]=%i",classes[i]);
  }
  return 1;
}

[Content of somedatafile.txt]

50 21 77 0 28 0 27 48 22 2
55 0 92 0 0 26 36 92 56 4
53 0 82 0 52 -5 29 30 2 1
37 0 76 0 28 18 40 48 8 1
37 0 79 0 34 -26 43 46 2 1
85 0 88 -4 6 1 3 83 80 5
56 0 81 0 -4 11 25 86 62 4
55 -1 95 -3 54 -4 40 41 2 1
53 8 77 0 28 0 23 48 24 4
37 0 101 -7 28 0 64 73 8 1
...

解决方案

This:

 sscanf (line,"%i",classes[i]);

is probably wrong. You need to dereference there too, try:

 sscanf (line,"%i", &(*classes)[i]);

This is because classes is a pointer to an array of integers. You want the address of one of those integers, so that sscanf() can write the parsed number there. Therefore, you must first dereference classes to get the array, then say that you want the address of element number i in that array.

You could also use

 sscanf (line,"%i", *classes + i);

Which might be clearer, depending on how comfortable you are with these things.

这篇关于在功能引起的malloc和sscanf赛格故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆