编码getline()实现-Valgrind错误 [英] Coding a getline() implementation - Valgrind errors

查看:66
本文介绍了编码getline()实现-Valgrind错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to recode an implementation of the getline() function, but using the file descriptor of the file and not a FILE *. I am only allowed to use malloc() and free(), along with 5 functions being 25 lines long at most. I think I've done correctly the project although I am a beginner in C and my code isn't probably good.

当我运行它时,它工作正常,但是valgrind显示我definetely lost x bytes,x取决于文件长度和READ_SIZE(在标题中定义的宏).

When I run it, it works fine, but valgrind shows that I definetely lost x bytes, x depending of the file length and the READ_SIZE (macro defined in the header).

根据valgrind的--leak-check=full,当我分配dest时,在str_realloc_cat函数中发生内存泄漏.我尝试过,但是找不到应该在哪里释放/做其他事情?

According to valgrind's --leak-check=full, I have a memory leak in the str_realloc_cat function, when I malloc dest. I tried but couldn't find where should I free / do something else?

以下是我的代码:

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

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

And a main functon if you would like to test:

#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. 您使用幻数remaining[i] == 10
  4. 您使用递归可以堆积堆栈return get_next_line(fd).没关系,我对您的尾部递归操作不太满意,只是请确保在编译时对此进行了优化.
  5. 您有意大利面条代码.
  1. You keep the buffer in a allocate memory
  2. You don't use a structure to regroup your variable
  3. You use magic number remaining[i] == 10
  4. You use recursive you can stack overflow return get_next_line(fd). Never mind, I didn't read well you have a tail recursive, just be sure to have the optimization on your compile for it.
  5. You have Spaghetti code.
  6. etc.

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

You should rewrite your whole function with a better logic first use this structure:

#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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆