C中的Malloc问题

0 c database memory malloc pointers

解决方案:在程序退出后释放已分配的内存.必须从磁盘读取+写回链表,然后重写以更新数据库!谢谢大家=)

您好,我基本上已经在这个数据库程序中工作了几天,但我只是不断达到死胡同.这项任务今天到期,所以如果你能帮助我,我将非常感激.= T

数据库使用链接列表实现,由几个文件组成:sdbm.c,sdbm.h,new.c,get.c,insert.c,put.c和remove.c.sdbm.c包含基于sdbm.h接口的数据库方法,其他文件包含使用sdbm方法的主要方法.

第一个问题来自insert程序,当我尝试添加一个键和值对时似乎工作正常......也就是说,直到我们再次尝试调用insert程序.分配的内存似乎已经消失了!我一直在研究,试图弄清楚为什么即使我有malloced,为什么它会在插入程序退出后消失.这是一些代码:

  • 节点结构+全局变量:
struct dbase_Node {
  char *keyValue;
  char *element;
  struct dbase_Node *next;
};

typedef struct dbase_Node Node;

Node *head;
Run Code Online (Sandbox Code Playgroud)

========

  • 插入方法
static bool sdbm_insert_back(Node **headRef, const char *key, const char *value)
{
  Node *new = (Node *)malloc(sizeof(Node));
  if (new == NULL)
    return false;
  else {
    new->keyValue = malloc(strlen(key));
    new->element = malloc(strlen(value));
    strcpy(new->keyValue, key);
    strcpy(new->element, value);

    new->next = *headRef;
    *headRef = new;
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)
  • 同步方法
bool sdbm_sync()
{
  if (!isOpen()) { return false; }

  if (fopen(databaseName, "w" ) == NULL) {
    error = SDBM_FOPEN_FAILED;
    return false;
  }

  Node *current = head;

  while (current != NULL) {
    fprintf(database, "Key: %s\n", current->keyValue);
    fprintf(database, "Value: %s\n", current->element);
    current = current->next;
  }
  return true;
}
Run Code Online (Sandbox Code Playgroud)

我执行以下操作:

./new [数据库] < - 工作正常./insert [数据库] [关键] [值] < - 似乎工作正常

然后我尝试插入更多,已经添加的节点消失了......

Thi*_*ter 5

new->keyValue = malloc(strlen(key));
new->element = malloc(strlen(value));
strcpy(new->keyValue, key);
strcpy(new->element, value);
Run Code Online (Sandbox Code Playgroud)

这会导致逐个缓冲区溢出.您需要为终止分配空间\0,因此请使用strlen(key) + 1strlen(value) + 1.

甚至更好,使用strdup():

new->keyValue = strdup(key);
new->element = strdup(value);
Run Code Online (Sandbox Code Playgroud)

程序存在后数据丢失的问题非常简单,但修复起来并不简单:您不是将其存储在内存中的任何位置.程序退出时,内存自动释放.所以你必须将它存储在一个文件中.