需要帮助清理C代码中的内存泄漏并解决分段错误问题,使用malloc和free。

huangapple go评论63阅读模式
英文:

Need help cleaning memory leaks and resolving segmentation fault in C code using malloc and free

问题

我一直在使用C语言编写这段代码,但是它有很多内存泄漏问题,我不知道如何清理。

当我在主函数中调用get_next_line时,我希望它能返回.txt文件的下一行内容。

有人可以帮助我吗?

我已经做了几次更改,但始终出现内存泄漏和分段错误。

我期望的只是读取文件。
英文:

I've been working on this code in C and it has lots of leaks that I don´t know how to clean.

What I want when I call get_next_line on main is for it to return the next line of a .txt file.

Can somebody help me pls?


size_t	ft_strlen(char *s)
{
	size_t	i;

	if (!s)
		return (0);
	i = 0;
	while (s[i] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') i++; return (i); } char *ft_strchr(char *s, int c) { size_t i; i = 0; if (!s) return (0); while ((s[i] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') && (s[i] != (unsigned char)c)) i++; if ((s[i] == (unsigned char)c) || (c == '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
')) return ((char *)&s[i]); return (0); } char *ft_substr(char *s, unsigned int start, size_t len) { size_t i; size_t j; char *sub; i = 0; j = 0; if (start >= ft_strlen(s) || !s || !len) { sub = malloc(1 * sizeof(char)); sub[0] = '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
'; return (sub); } while (s[start + j] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
' && j < len) j++; if (s[start + j] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') j++; sub = malloc((j + 1) * sizeof(char)); if (!sub) { free(s); return (0); } while (s[start] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
' && j > i) sub[i++] = s[start++]; sub[i] = '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
'; //free(s); return (sub); } char *ft_strjoin(char *s1, char *s2) { char *cat; size_t i; size_t j; i = 0; j = 0; if (!s1) { s1 = malloc(sizeof(char) * 1); s1[0] = '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
'; } cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); if (!cat) return (NULL); while (s1[i] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') { cat[i] = s1[i]; i++; } while (s2[j] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') cat[i++] = s2[j++]; free(s1); cat[i] = '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
'; return (cat); } char *ft_content(int fd, char *content) { char *buf; int buf_nb; buf_nb = 1; buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char)); if (!buf) return (0); while ((!ft_strchr(content, '\n')) && buf_nb != 0) { buf_nb = read(fd, buf, BUFFER_SIZE); if (buf_nb == -1) { free(buf); free(content); return (NULL); } buf[buf_nb] = '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
'; content = ft_strjoin(content, buf); } free(buf); return (content); } char *get_next_line(int fd) { static char *content; char *sub; int i; int len; i = 0; if (fd < 0 || BUFFER_SIZE <= 0) return (NULL); content = ft_content(fd, content); if (content == NULL) return (NULL); if (content[0] == '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') return (NULL); while (content[i] != '\n' && content[i] != '

size_t	ft_strlen(char *s)
{
size_t	i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char	*ft_strchr(char *s, int c)
{
size_t	i;
i = 0;
if (!s)
return (0);
while ((s[i] != '\0') && (s[i] != (unsigned char)c))
i++;
if ((s[i] == (unsigned char)c) || (c == '\0'))
return ((char *)&s[i]);
return (0);
}
char	*ft_substr(char *s, unsigned int start, size_t len)
{
size_t	i;
size_t	j;
char	*sub;
i = 0;
j = 0;
if (start >= ft_strlen(s) || !s || !len)
{
sub = malloc(1 * sizeof(char));
sub[0] = '\0';
return (sub);
}
while (s[start + j] != '\0' && j < len)
j++;
if (s[start + j] != '\0')
j++;
sub = malloc((j + 1) * sizeof(char));
if (!sub)
{
free(s);
return (0);	
}
while (s[start] != '\0' && j > i)
sub[i++] = s[start++];
sub[i] = '\0';
//free(s);
return (sub);
}
char	*ft_strjoin(char *s1, char *s2)
{
char	*cat;
size_t	i;
size_t	j;
i = 0;
j = 0;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
cat = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!cat)
return (NULL);
while (s1[i] != '\0')
{
cat[i] = s1[i];
i++;
}
while (s2[j] != '\0')
cat[i++] = s2[j++];
free(s1);
cat[i] = '\0';
return (cat);
}
char	*ft_content(int fd, char *content)
{
char	*buf;
int		buf_nb;
buf_nb = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while ((!ft_strchr(content, '\n')) && buf_nb != 0)
{
buf_nb = read(fd, buf, BUFFER_SIZE);
if (buf_nb == -1)
{
free(buf);
free(content);
return (NULL);
}
buf[buf_nb] = '\0';
content = ft_strjoin(content, buf);
}
free(buf);
return (content);
}
char	*get_next_line(int fd)
{
static char	*content;
char		*sub;
int			i;
int			len;
i = 0;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
content = ft_content(fd, content);
if (content == NULL)
return (NULL);
if (content[0] == '\0')
return (NULL);
while (content[i] != '\n' && content[i] != '\0')
i++;
sub = ft_substr(content, 0, i);
len = ft_strlen(content);
i++;
content = ft_substr(content, i, len);
return (sub);
}
int    main(void)
{
int    a = open("exemplo.txt", O_RDONLY);
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
printf("%s", get_next_line(a));
return (0);
}
') i++; sub = ft_substr(content, 0, i); len = ft_strlen(content); i++; content = ft_substr(content, i, len); return (sub); } int main(void) { int a = open("exemplo.txt", O_RDONLY); printf("%s", get_next_line(a)); printf("%s", get_next_line(a)); printf("%s", get_next_line(a)); printf("%s", get_next_line(a)); return (0); }

I've done several changes but I always get memory leaks and segmentation fault.

What I expected was just to read the file.

答案1

得分: 1

如果您正在使用gcc或clang编译器,您可以尝试在编译程序时使用命令行选项-fsanitize=leak,以检测任何内存泄漏。

有关更多信息,请参阅gcc手册中的此页面

英文:

If you are using the compilers gcc or clang, you can try using the command-line option -fsanitize=leak when compiling the program, in order to detect any memory leaks.

See this page from the gcc manual for further information.

huangapple
  • 本文由 发表于 2023年6月1日 23:47:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定