gpt4 book ai didi

编码 getline() 实现 - Valgrind 错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:13 25 4
gpt4 key购买 nike

我必须重新编写 getline() 函数的实现,但使用文件的文件描述符而不是 FILE *。我只能使用 malloc()free(),以及最多 25 行的 5 个函数。我认为我已经正确地完成了这个项目,尽管我是 C 语言的初学者而且我的代码可能不太好。

当我运行它时,它工作正常,但 valgrind 显示我确实丢失了 x 字节,x 取决于文件长度和 READ_SIZE( header 中定义的宏)。

根据 valgrind 的 --leak-check=full,当我 malloc dest 时,我在 str_realloc_cat 函数中有内存泄漏。我试过了,但找不到我应该在哪里释放/做其他事情?

下面是我的代码:

char *get_next_line(const int fd)
{
static char *remaining = "";
char *buffer;
ssize_t cread;
size_t i;

i = 0;
if (remaining == NULL)
return (NULL);
if ((buffer = malloc(SOF(char) * READ_SIZE + 1)) == NULL ||
(cread = read(fd, buffer, READ_SIZE)) < 0)
return (NULL);
buffer[cread] = 0;
remaining = str_realloc_cat(remaining, buffer);
while (remaining[i])
{
if (remaining[i] == 10)
{
remaining[i] = 0;
buffer = str_create_cpy(remaining);
remaining = remaining + i + 1;
return (buffer);
}
i++;
}
return (check_eof(fd, buffer, remaining, cread));
}

char *str_realloc_cat(char *rem, char *buf)
{
size_t i;
size_t dest_i;
char *dest;

i = (dest_i = 0);
if ((dest = malloc(SOF(char) * (str_len(rem) + str_len(buf) + 1))) == NULL)
return (NULL);
while (rem[i])
{
dest[dest_i] = rem[i];
dest_i++;
i++;
}
i = 0;
while (buf[i])
{
dest[dest_i] = buf[i];
dest_i++;
i++;
}
dest[dest_i] = 0;
free(buf);
return (dest);
}

char *check_eof(const int fd, char *buffer, char *remaining, ssize_t cread)
{
if (cread == 0)
return (NULL);
if (cread < READ_SIZE)
{
buffer = remaining;
remaining = NULL;
return (buffer);
}
return (get_next_line(fd));
}

char *str_create_cpy(const char *src)
{
char *dest;
size_t i;

i = 0;
if ((dest = malloc(sizeof(char) * str_len(src) + 1)) == NULL)
return (NULL);
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = 0;
return (dest);
}

int str_len(const char *str)
{
size_t i;

i = 0;
while (str[i])
i++;
return (i);
}

如果您想测试,还有一个主要功能:

#define SOF(x) sizeof(x) // Why in the comments

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

UNUSED(ac);
if (!av[1])
return 1;
fd = open(av[1], O_RDONLY);
while ((s = get_next_line(fd)))
{
printf("%s\n", s);
free(s);
}
close(fd);
}

最佳答案

你的算法不好:

  1. 您将缓冲区保存在分配的内存中
  2. 您不使用结构来重新组合您的变量
  3. 你使用魔数(Magic Number)remaining[i] == 10
  4. 您使用递归可以堆栈溢出return get_next_line(fd)。没关系,我没看懂你有尾递归,只要确保对你的编译进行优化即可。
  5. 您有 Spaghetti 代码。
  6. 等等

你应该首先使用这个结构用更好的逻辑重写你的整个函数:

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

#define GNL_SIZE 4096

struct gnl_context {
char buffer[GNL_SIZE];
size_t i;
size_t read;
};

char *get_next_line_r(int fd, struct gnl_context *gnl_context);
char *get_next_line(int fd);

static char *read_buffer(struct gnl_context *gnl_context, char *str,
size_t *size) {
size_t i = gnl_context->i;
while (i < gnl_context->read && gnl_context->buffer[i] != '\n') {
i++;
}
size_t j = i - gnl_context->i;

char *ret = realloc(str, *size + j + 1);
if (ret == NULL) {
return NULL;
}
memcpy(ret + *size, gnl_context->buffer + gnl_context->i, j);
*size += j;
ret[*size] = '\0';
gnl_context->i = i;

return ret;
}

char *get_next_line_r(int fd, struct gnl_context *gnl_context) {
char *str = NULL;
size_t size = 0;
loop:
if (gnl_context->i == gnl_context->read) {
ssize_t ret = read(fd, gnl_context->buffer, GNL_SIZE);
if (ret <= 0) {
return str;
}
gnl_context->read = (size_t)ret;
gnl_context->i = 0;
}

char *tmp = read_buffer(gnl_context, str, &size);
if (tmp == NULL) {
return str;
}
if (gnl_context->i != gnl_context->read) {
gnl_context->i++;
return tmp;
}
str = tmp;
goto loop;
}

char *get_next_line(int fd) {
static struct gnl_context gnl_context;
return get_next_line_r(fd, &gnl_context);
}

int main(void) {
char *str;
while ((str = get_next_line(0)) != NULL) {
printf("%s\n", str);
free(str);
}
}

关于编码 getline() 实现 - Valgrind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41655671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com