Segmentation fault in function read_line

0

I have a function that returns a string of a line from a file, but when I execute it, I get a segmentation fault message. Here's the code:

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "parser.h"

const int MAX_CHAR_PER_LINE = 100;

int main()
{
    FILE *h = fopen("/home/test.txt", "r");
    char *line = read_line(h);

    printf("%s", line);

    return 0;
}

int parse(FILE *header)
{

   return 0;
}

char* read(FILE *header, int bytes)
{
    char *ret_v = (char*) malloc(bytes);
    fread((void*) ret_v, 1, bytes, header);

    return ret_v;
}

char* read_line(FILE *header)
{
    char *line = (char*) malloc(MAX_CHAR_PER_LINE);
    char ch = getc(header);
    int count = 0;

    while (ch != 255 && ch != '\n')
    {
        line[count] = ch;
        ch = getc(header);
        count++;
    }
    line[++count] = '
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "parser.h"

const int MAX_CHAR_PER_LINE = 100;

int main()
{
    FILE *h = fopen("/home/test.txt", "r");
    char *line = read_line(h);

    printf("%s", line);

    return 0;
}

int parse(FILE *header)
{

   return 0;
}

char* read(FILE *header, int bytes)
{
    char *ret_v = (char*) malloc(bytes);
    fread((void*) ret_v, 1, bytes, header);

    return ret_v;
}

char* read_line(FILE *header)
{
    char *line = (char*) malloc(MAX_CHAR_PER_LINE);
    char ch = getc(header);
    int count = 0;

    while (ch != 255 && ch != '\n')
    {
        line[count] = ch;
        ch = getc(header);
        count++;
    }
    line[++count] = '%pre%';

    char *ret_line;
    memcpy(ret_line, line, strlen(line));
    free(line);

    return ret_line;
}
'; char *ret_line; memcpy(ret_line, line, strlen(line)); free(line); return ret_line; }

I do not understand why you are making this mistake, apparently I did everything right. (I.e. Note: I just discovered that the "error" is not in the code, but in the compiler. I compiled with the TCC and the segfault error appeared, but when compiled with gcc the function returned the correct value.

    
asked by anonymous 09.06.2018 / 21:09

1 answer

0

There should be no problem with your compiler. The problem with your code is at the end of the read_line function:

char *ret_line;
memcpy(ret_line, line, strlen(line));
free(line);
return ret_line;

The signature of the memcpy function is as follows:

void * memcpy ( void * destination, const void * source, size_t num );

What the memcpy function does is to copy num bytes to the address pointed by source and places those bytes in the address pointed by destination . This means that memcpy does not allocate memory for the destination pointer. You are copying bytes to an uninitialized pointer. This is the cause of segmentation fault . So the correct thing would be to do this:

char *ret_line = (char*) malloc(strlen(line)); 
memcpy(ret_line, line, strlen(line));
free(line);
return ret_line;

But to copy strings there is a better alternative, which would be strcpy . The code would look pretty much the same, you just would not have to specify the number of bytes, because the function cope until you find the terminating character.

But in fact you do not even need any of this, you could return the line pointer. And do not forget to free memory in the main function.

    
10.06.2018 / 20:14