堆指针不会被分配给数组有什么原因吗
Is there any reason that a heap pointer would not get assigned to an array?
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,因此它不可能指向包含4113
的Node
。
我不认为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}
相关文章:
- 通过JNI传递数据数组的最快方法是什么
- 讨论 - 创建矩阵时的数组与向量的向量 - 什么是最实用的选择
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 当该数组的索引中没有元素时,指针指向什么?
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 从字符数组的元素中减去'a'是什么意思
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 字符数组前面的加号是什么意思?
- 初始化数组、"memset"或" {//value} "的最佳方法是什么?
- 在C++中,建议通过数组循环的方式是什么?
- C++ 未初始化的本地(非全局)int 数组中的元素类型到底是什么?
- C++数组中的初始值设定项是什么?
- 如果我在字符数组上使用 close() 会发生什么?
- 指向数组基址的指针而不是指向第一个元素的指针有什么优点?
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 初始化不是整数的巨大常量多维数组的最佳方法是什么?
- 这是什么数组声明
- 什么数组结构在c#中有,而在C/ c++ /Java中没有