【问题标题】:How to resolve free(): invalid pointer error while assigning multiple variables in a doubly linked list?如何解决free():在双向链表中分配多个变量时出现无效指针错误?
【发布时间】:2021-05-09 19:46:57
【问题描述】:

这里是 C++ 新手。

我的双向链表中有 2 个数据变量; instr_num 和操作码。当我将一个值复制到 instr_num 中时,它可以工作,但是当我为操作码执行此操作时会引发错误。

struct Node {
    int instr_num;
    std::string opcode;
    struct Node* next;
    struct Node* prev;
};

void initialize_DLL(Node** tail, Node** head, int s_instr_num, string s_opcode) {
    Node* new_node = (Node*) malloc(sizeof(Node));
    if (new_node == NULL) {
        exit(1);
        return;
    }
    
    new_node->instr_num = s_instr_num; // THIS EXECUTES
    new_node->opcode = s_opcode;        // THIS THROWS AN ERROR:  free(): invalid pointer
    new_node->prev = NULL;
    new_node->next = NULL;
    
    *tail = new_node;
    *head = new_node;
}

int main(){
    Node* tail = NULL;
    Node* head = NULL;
    std::string temp_opcode = "ADD"
    
    initialize_DLL(&tail, &head, 1, temp_opcode);
    return 0;
}

我猜它可能与 malloc 相关,但我不确定。我做错了什么?

【问题讨论】:

  • 您分配的Node 中的任何内容都不会被初始化。在 C++ 中使用 new
  • struct int instr_num; 这还能编译吗?
  • 基本上你有未定义的行为,没有人可以仅根据这些信息重现。
  • 你为什么使用malloc?这不是您创建实例的方式。 malloc 非常特殊,用例非常少见。如果您是初学者,您可能会忘记它存在很长一段时间。我从来不用它
  • @Zoso 我的错。我之前在那里有一个结构,当我发布它时错过了删除它。它只是 int instr_num。

标签: c++ string doubly-linked-list


【解决方案1】:

在 C++ 中,您可以使用 new 来初始化分配的内存:

#include <string>
#include <utility>

struct Node {
  int instr_num;
  std::string opcode;
  Node* next;
  Node* prev;
};

void initialize_DLL(Node** tail, Node** head, int s_instr_num,
                    std::string s_opcode) {
  *tail = *head =
      new Node{s_instr_num, std::move(s_opcode)};  // NULLs are implicit
}

int main() {
  Node* tail = NULL;
  Node* head = NULL;
  std::string temp_opcode = "ADD";

  initialize_DLL(&tail, &head, 1, temp_opcode);

  delete tail;  // don't leak
}

请注意,使用原始指针是危险的。

在 C 中,您可以使用 flexible array member:

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

struct Node {
  int instr_num;
  struct Node* next;
  struct Node* prev;
  char opcode[];
};

void initialize_DLL(struct Node** tail, struct Node** head, int s_instr_num,
                    char const* s_opcode) {
  size_t str_sz = strlen(s_opcode) + 1;
  *tail = *head = malloc(sizeof(struct Node) + str_sz);

  if (!*head)
    return;

  (*head)->instr_num = s_instr_num;
  (*head)->next = NULL;
  (*head)->prev = NULL;
  memcpy((*head)->opcode, s_opcode, str_sz);
}

int main() {
  struct Node* tail = NULL;
  struct Node* head = NULL;
  char const* temp_opcode = "ADD";

  initialize_DLL(&tail, &head, 1, temp_opcode);

  free(head);
}

C++ 中不存在“灵活数组成员”的概念。您可以使用 1 个字符的数组来模拟类似的东西,但根据 C++ 标准,索引其他字符将是未定义的行为。所以,不要在 C++ 中使用它,除非你的编译器明确允许并且你可以使用不可移植的代码。

【讨论】:

  • 非常感谢。我在赶上C++,我会更加关注我在C++中不应该做的事情。
  • 我有一个后续问题。如果我将原始结构更改为:``` struct Node { struct my_data;下一个节点*;节点* 上一个; };结构 my_data { int instr_num; std::string 操作码; }; ```如何使用new创建初始化分配的内存?
  • @ObviousActivity936 gcc.godbolt.org/z/z8Wo4K1cY
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 2018-09-17
  • 1970-01-01
相关资源
最近更新 更多