堆指针不会被分配给数组有什么原因吗

Is there any reason that a heap pointer would not get assigned to an array?

本文关键字:什么 数组 指针 分配      更新时间:2023-10-16

Node的结构如下:

struct Node {
int data;
Node *next;
bool someBool;
};

我有以下几行:

Node *hello = new Node{data, cur, !new_head}; // line A
array[new_head_index] = hello;                // line B
}                                                 // line C

GDB在所有3条线上确认new_head_index的值为1。

GDB确认,在A、B和C行,当我执行p *hello(打印hello的内容(时,我得到:

(gdb) p *hello
$7 = {data = 888, next = 0x8414e70, someBool = false}

但是打印array@2(数组长度为2,主要声明为Node *heads[numHeads] = {new Node{0, nullptr, false}, nullptr};(的内容在B行和C行(在实际执行行之前(有这样的内容:

(gdb) p **array@2
$8 = {{data = 777, next = 0x8414e70, someBool = true}, {data = 33, next = 0x378, someBool = 112}}

(777节点是预期的,我之前填写过(。

在A线上,它有:

(gdb) p **array@2
$9 = {{data = 777, next = 0x8414e70, someBool = true}, {data = 61265, next = 0x0, someBool = false}}

从本质上讲,hello没有被分配给array[1]。这可能是什么原因?

以下是一个可重复的最小示例:main.cc

#include <iostream>
#include <string>
#include "new_mod.h"
using namespace std;
int len(Node **array) {
int i = 0, count = 0;
while (array[i++]) { ++count; }
return count;
}
void attach(Node **array, int head, int index, int data) {
Node *hello = new Node{data, cur};
array[new_head_index] = hello;
}
int main() {
string command;
int head;
Node *array[numHeads] = {new Node{0, nullptr}, nullptr};
while (cin >> command) {
if (command == "a") {
int m, x;
cin >> head >> m >> x;
attach(array, head, m, x);
}
}
}

型号h

#ifndef MOD_H
#define MOD_H
#include <ostream>
struct Node {
int data;
Node *next;
bool someBool = false;
};
const int numHeads = 2;
void attach(Node **array, int head, int index, int data);
#endif

尝试输入此输入(我已将此文件命名为a.in(

a 0 0 777
a 0 1 888

g++ -Wall -g main.cc -o newe编译,这样你就可以用gdb做事情了!

顺便说一句,以下是我在gdb中为解决上述问题所做的:

gdb newe
b attach(Node**, int, int, int)
run <a.in
layout n
c
n
n
n
n
n
n
n          (comment: I did n until line Node *hello = new Node{data, cur};)
p **array@2
n
n
p **array@2 (the problem is shown)

问题是Node *array[2]Node **array不是一回事,当您向GDB请求p **array@2时,您要求将数组解释为array是双指针,而不是数组。

在开始输入循环之前,您可以观察到这一点:

(gdb) p *array@2
$1 = {0x55555556ae70, 0x0}
(gdb) p *array[0]
$2 = {data = 0, next = 0x0, someBool = false}

在这里,您可以看到array处于预期状态:第一个节点全为零,第二个节点为NULL。

但当你这样做时:

(gdb) p **array@2
$3 = {{data = 0, next = 0x0, someBool = false}, {data = 4113, next = 0x3737203020302061, someBool = 55}}

您可以立即看到您没有看到正确的数据:您知道array[1]为NULL,因此它不可能指向包含4113Node

我不认为print *array[0 .. N]有GDB内置语法。一种可能的解决方案是定义一个辅助函数(可以很容易地将其泛化为将数组名称和大小作为参数,但为了简单起见,这里保持为琐碎的(:

(gdb) def parray
Type commands for definition of "parray".
End with a line saying just "end".
>print *array[0]
>print *array[1]
>end
(gdb) parray
$4 = {data = 0, next = 0x0, someBool = false}
Cannot access memory at address 0x0
(gdb) c
Continuing.
Breakpoint 1, attach (array=0x7fffffffdb70, head=0, index=1, data=888) at foo.cc:34
34          array[new_head_index] = hello;
(gdb) n
35      }
(gdb) parray
$5 = {data = 777, next = 0x55555556ae70, someBool = false}
$6 = {data = 888, next = 0x55555556ae70, someBool = false}