分段错误(核心转储)错误,没有行引用

时间:2021-11-26 16:59:13

I am receiving the "Segmentation fault (core dumped)" error. This code was working earlier, and I am at a lost what is causing this. Any pointers would be greatly appreciated. This error provides no line reference either.

我收到“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] = '\0';

        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;
}

3 个解决方案

#1


3  

The problem here is, you're using double *Xflat; uninitialized. Accessing uninitalized memory invokes undefined behaviour which in turn may cause segmantation fault.

这里的问题是,你使用的是double * Xflat;初始化。访问未初始化的内存会调用未定义的行为,这反过来可能会导致分段错误。

You need to allocate memory to double *Xflat; before using it.

你需要分配内存来加倍* Xflat;在使用它之前。

A Suggestion: Enable -g flag while compiling and run your binary through a debugger like gdb. Most of the time in pinpoints the error to the specific line number itself.

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

#2


2  

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

Here Xflat is uninitialized and you are trying to write to some memory which is not allocated so the behavior is undefined and you need to allocate memory for your pointer before writing something to it.

这里Xflat是未初始化的,你试图写入一些未分配的内存,因此行为是未定义的,你需要在向它写入内容之前为你的指针分配内存。

#3


0  

in function assignX(),

在函数assignX()中,

the passed parameters nRow and nCol are arbitrary/hardcoded values that have nothing to do with the actual input file contents.

传递的参数nRow和nCol是与实际输入文件内容无关的任意/硬编码值。

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.

#1


3  

The problem here is, you're using double *Xflat; uninitialized. Accessing uninitalized memory invokes undefined behaviour which in turn may cause segmantation fault.

这里的问题是,你使用的是double * Xflat;初始化。访问未初始化的内存会调用未定义的行为,这反过来可能会导致分段错误。

You need to allocate memory to double *Xflat; before using it.

你需要分配内存来加倍* Xflat;在使用它之前。

A Suggestion: Enable -g flag while compiling and run your binary through a debugger like gdb. Most of the time in pinpoints the error to the specific line number itself.

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

#2


2  

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

Here Xflat is uninitialized and you are trying to write to some memory which is not allocated so the behavior is undefined and you need to allocate memory for your pointer before writing something to it.

这里Xflat是未初始化的,你试图写入一些未分配的内存,因此行为是未定义的,你需要在向它写入内容之前为你的指针分配内存。

#3


0  

in function assignX(),

在函数assignX()中,

the passed parameters nRow and nCol are arbitrary/hardcoded values that have nothing to do with the actual input file contents.

传递的参数nRow和nCol是与实际输入文件内容无关的任意/硬编码值。

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.