Segmentation fault (core dumped) 错误,没有行引用

Segmentation fault (core dumped) Error, no line reference

我收到 "Segmentation fault (core dumped)" 错误。这段代码之前工作正常,我不知道是什么原因造成的。任何指针将不胜感激。此错误也没有提供行参考。

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

int assignX(int nCol, int nRow, double *Xflat, char *fileName);

int main(){
   FILE *f;
   char myStr[1000];
   int strL;
   int nCol;
   int nRow;
   char *fileName = "reg.dat";
   int i, j, k, z, n1=nCol, n2=1, info;

   double *Xflat;
   double *temp;

   f = fopen(fileName, "r");
   if (f == NULL) perror ("Error opening file");
   else {
     if (fgets(myStr, 1000, f) != NULL )
       puts(myStr);
     fclose(f);
   }

   strL = strlen(myStr);
   nCol = 3;
   nRow = 150;
   printf("Sample size and number of predictors are %d and %d respectively.\n", nRow, nCol-1);

   assignX(nCol, nRow, Xflat, fileName);

   return 0;
}

int assignX(int nCol, int nRow, double *Xflat, char *fileName){
  int i=0;
  int j;
  int k=0;
  char string[1000];
  char* data = NULL;
  FILE *f;
  f = fopen(fileName, "r");

  while(fgets(string, sizeof(string), f) != NULL){
    data = strtok(string, " ");
    for (j=0; NULL != data && j<nCol; j++){
        if (data[strlen(data) - 1] == '\n')
            data[strlen(data) - 1] = '[=10=]';

        if (j!=0){
          Xflat[i] = atof(data);
          i++;
        }
        data = strtok(NULL, " ");
    }
  }

  for (i=0;i<(nRow*(nCol-1));i++){
    printf("%f\n", Xflat[i]);
  }

  return 0;
}

这里的问题是,您使用的 double *Xflat; 未初始化。访问未初始化的内存调用 undefined behaviour,这反过来 可能 导致分段错误。

使用前需要给double *Xflat;分配内存。

建议:在编译时启用 -g 标志,并通过 gdb 等调试器 运行 您的二进制文件。大多数情况下,将错误定位到特定行号本身。

   if (j!=0){
       Xflat[i] = atof(data);
       i++;
        }

此处 Xflat 未初始化,您正在尝试写入一些未分配的内存,因此行为未定义,您需要在向其写入内容之前为指针分配内存。

在函数 assignX() 中,

传递的参数 nRow 和 nCol 是 arbitrary/hardcoded 与实际输入文件内容无关的值。

Therefore, the code block beginning with 
'for (i=0;i<(nRow*(nCol-1));i++){'  
 has nothing to do with the number of entries 
 saved in the Xflat[] array.

 Therefore, the printf() will either not print all the Xflat[] entries
 or 
 will print from uninitialized memory, 
 resulting in undefined behaviour 
 and can/will lead to a seg fault event. 

suggest: 
1) do not pass the xRow and xCol variables
2) use for( int j=0; j<i; j++ )

There is also the problem that the 'double *Xflat'
does not point to any valid/allocated memory.

suggest:  in main()
double * Xflat = NULL;

and in the assignX() function,
use realloc() and the value 'i+1' 
before each setting of any Xflat[i] offset to a value
which will result in many (expensive) calls to realloc()
An alternative method would be only call realloc()
when all the available 'double' slots are filled
and realloc() use double the current number of 'double' slots
(remembering that the size value would be: sizeof double * numberSlots)

Also, the pointer Xflat is not used anywhere in main() 
so suggest removing from main and from the assignX parameter list
the Xflat declaration can be moved to the assignX function

the main variable 'temp' is not used, so should be eliminated
the main variable 'strL' is only set, but not used
    so should be eliminated

there are some other problems, 
enable all the warnings when compiling.  Then fix the warnings.