简体   繁体   中英

C - “Segmentation fault: 11” running program

This program returns "Segmentation fault: 11" when I run it. The compiler (GCC) doesn't return any error or warning.

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

The content of file.dat is:

12
08
1990

I've debugged it with GDB and this is the result:

(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

What does that mean and what can I do to solve the problem? Thank you!

Notice that while using malloc would work, it's unecessary extra work for you since you will also have to call free later when you no longer need to use the data struct. But for that you should make the variable local to main() .

This should solve your problem

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

You don't need to declare data as a pointer in this particular case, and it would be better if you don't until you understand dynamic memory allocation correctly, also, the data variable does not need to be global, you can declare it inside the main() function.

The segmentation fault is caused by an invalid pointer dereference, if you want to use a pointer then this is how you should do it

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

You forgot to allocate memory to your structure

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

You should allocate memory to your pointer before writing something to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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