繁体   English   中英

C-“段故障:11”运行程序

[英]C - “Segmentation fault: 11” running program

运行该程序时,它会返回“ Segmentation fault:11”。 编译器(GCC)不返回任何错误或警告。

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

typedef struct {
  char day[3];
  char month[3];
  char year[5];
} DATA;

DATA *data;

int main()
{
  FILE *file;
  char line_buffer[BUFSIZ];

  if (!(file = fopen("file.dat", "rt")))
  { 
    printf ("Something went wrong while opening the file.\n");
  }
  else
  {
    int line_number = 0;

    while (fgets(line_buffer, sizeof(line_buffer), file))
    {
      ++line_number;

      if      (line_number == 1) { strncpy(data->day,   line_buffer, 2); }
      else if (line_number == 2) { strncpy(data->month, line_buffer, 2); }
      else if (line_number == 3) { strncpy(data->year,  line_buffer, 4); }
    }

    printf("Content: %s-%s-%s\n", data->day, data->month, data->year); 
  }
  return 0;
}

file.dat的内容是:

12
08
1990

我已经使用GDB对其进行了调试,结果如下:

(gdb) run
Starting program: /Users/macuser/Desktop/Primitiva/Proyecto/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00007fff949413a0 in _dispatch_queue_attrs () from /usr/lib/system/libdispatch.dylib

这是什么意思,我该怎么解决? 谢谢!

注意,尽管使用malloc可以工作,但对您来说却是不必要的额外工作,因为以后当您不再需要使用data结构时,您还必须调用free 但是为此,您应该将变量设置为main()局部变量。

这应该可以解决您的问题

typedef struct {
char day[3];
char month[3];
char year[5];
} DATA;

DATA data;
/*  ^ no star here, because you don't need a pointer in your particular case */

int main()
{
FILE *file;
char line_buffer[BUFSIZ];

if (!(file = fopen("file.dat", "rt")))
{
    printf ("Something went wrong while opening the file.\n");
}
else
{
    int line_number = 0;

    while (fgets(line_buffer, sizeof(line_buffer), file))
    {
    ++line_number;

    if      (line_number == 1) { strncpy(data.day,   line_buffer, 2); }
    else if (line_number == 2) { strncpy(data.month, line_buffer, 2); }
    else if (line_number == 3) { strncpy(data.year,  line_buffer, 4); }
    }

    printf("Content: %s-%s-%s\n", data.day, data.month, data.year);
}
return 0;
}

在这种特殊情况下,您不需要将data声明为指针,如果您不了解正确的动态内存分配就更好了,而且, data变量不需要是全局变量,则可以声明它在main()函数中。

分段错误是由无效的指针取消引用引起的,如果要使用指针,则应该这样做

typedef struct {
char day[3];
char month[3];
char year[5];
} DATA;

int main()
{
FILE *file;
char line_buffer[BUFSIZ];
if (!(file = fopen("file.dat", "rt")))
{
    printf ("Something went wrong while opening the file.\n");
}
else
{
    int line_number = 0;
    DATA *data;

    /* 
     * here data is an invalid pointer, it points nowhere. 
     * to make it valid you need malloc
     */
    data = malloc(sizeof(*data));
    /* on failure, malloc returns NULL, for example when there is no more memory left in the system 
     * so one should check for the return value
     */
    if (data == NULL)
    {
        fclose(file);
        return -1;
    }
    /* now data is a valid pointer you can derefrence it */

    while (fgets(line_buffer, sizeof(line_buffer), file))
    {
    ++line_number;

    if      (line_number == 1) { strncpy(data->day,   line_buffer, 2); }
    else if (line_number == 2) { strncpy(data->month, line_buffer, 2); }
    else if (line_number == 3) { strncpy(data->year,  line_buffer, 4); }
    }

    printf("Content: %s-%s-%s\n", data->day, data->month, data->year);
    /* after you have finished using the pointer, call free */
    free(data);
}
return 0;
}

您忘记为您的结构分配内存

DATA *data = malloc(sizeof(DATA));

您应该在向指针写入内容之前为其分配内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM