0

I tried to learn C language. I succeed to create a program which reads line by line and returns it to the terminal. Unfortunately I still have some leaks problem when I check with valgrind. Do you have some tips?

#include "get_next_line.h"
#include "./libft/libft.h"

int     ft_check_error(char **line, char **stock, int fd)
{
    if (line == NULL || fd < 0 || BUFF_SIZE < 1)
        return (-1);
    if (!*stock)
    {
        if (!(*stock = ft_strnew(BUFF_SIZE + 1)))
            return (-1);
    }
    return (0);
}



char    *ft_read(char *stock, char *buf)
{
    char *tmp;

    tmp = stock;
    stock = ft_strjoin(stock, buf);
    ft_strdel(&tmp);

    return (stock);
}

int     get_next_line(const int fd, char **line)
{
    static  char    *stock[1024];
            char    buf[BUFF_SIZE + 1];
            int     ret;
            int     i;
if (ft_check_error(line, &stock[fd], fd) == -1)
    return (-1);

while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{
    buf[ret] = '\0';
    stock[fd] = ft_read(stock[fd], buf);
}
 i = 0;
 if (stock[fd][i])
{
    while (stock[fd][i] != '\n' && stock[fd][i])
        i++;
    *line = ft_strsub(stock[fd], 0, i);

    free(*line); 
    stock[fd] = &stock[fd][i + 1];
    return (1);
}
 if (ret == -1)
    return (-1);
    return (0);
}

And here is the main function:

int main(int ac, char **av)
{
    int     fd;
    char    *line;

    line = NULL;
    fd = open(av[ac-1], O_RDONLY);

    while (get_next_line(fd, &line) == 1)
    {
        printf(RED "\nResultat: \n" RESET);
        ft_putstr(line);
    }

    close(fd);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Please provide a complete code example, you’re missing the main part. Maybe provide a snippet from the Valgrind report.... compile your program with `gcc -g` so Valgrind can print more degug info. `line` is a double pointer passed into your function, which means you would free each line with something like `free(line[i])`, before then freeing the top level pointer later on with `free(line)`. At the monument you’re passing in the `value` of `line` to `free()` –  Feb 11 '18 at 10:46
  • I add the main part in my post. I not sure for the free line[i] because I free each line before return it. –  Feb 12 '18 at 08:10
  • We have no way of knowing what is in `"./libft/libft.h"` so how memory is handled there is unclear. – David C. Rankin Feb 12 '18 at 08:18
  • Only a buff size of 20 but we can change by what we want. –  Feb 12 '18 at 08:46

1 Answers1

1

If you need to pass in a double pointer then you’ll first need to declare one;

char *ptr
char **line

ptr = malloc(sizeof(?how much memory?)) 

line = &ptr

A pointer to pointer variable can hold the address of another pointers variable.

Then in your function/s you use the double pointer and also free In your function.

free(line)

Please see How do pointer to pointers work in C? for a really good answer regarding how double pointers work

Or,

You could allocate the memory within you function, which will cause you the least amount of change

In main

char **line;

Then;

int get_next_line(const int fd, char **line) {

    *line = malloc(sizeof(?how much memory?));

    free(*line)
  • For the training it is necessary to use char **line and I can't free it in a main because my job is only to make a get next line correctly free. –  Feb 12 '18 at 08:49
  • But char *stock[fd] it is already a pointer no ? Because the program works very well, I have only some memory loss –  Feb 12 '18 at 09:28
  • @Brugher you’ve not provided any info on what ` *lft_strsub(stock[fd], 0, i);` is, or does, or even returns. I’ve updated my answer with an alternative way –  Feb 12 '18 at 09:45
  • strsub allow memory for put in line my stock readed or until the '\n'. So I allocate already memory for that. –  Feb 12 '18 at 09:59
  • @Brugher please see my alt answer –  Feb 12 '18 at 10:00
  • Have you tried my suggestion? ... So your `strsub` changes `*line` into a double pointer, allocates it a valid address space and then assigns the return value back to itself... –  Feb 12 '18 at 10:05
  • Yes exactly that –  Feb 12 '18 at 10:20