gpt4 book ai didi

c++ - 将指针分配给指针并不能使它们相等

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:22 26 4
gpt4 key购买 nike

我有一个很大的 unicode 代码点描述静态 HashMap 。每个散列值都指向一个以空字符结尾的指向元素的指针数组。以下是我的访问函数:

extern CodepointInfo ***codepoint_table;

uint32_t fnv1a(uint32_t input) { ... } // hash function

const CodepointInfo CodepointInfo::get(uint32_t codepoint) {
uint32_t hash = fnv1a(codepoint) % 30000 // Number of buckets of the hashmap
CodepointInfo **bucket = codepoint_table[hash];
for(uint32_t i = 0; bucket[i] != nullptr; i++) {
if(bucket[i]->codepoint == codepoint)
return *(codepoint_table[hash][i]);
}
return {codepoint, "unassigned", GeneralCategory::UNASSIGNED, 0, BidiClass::L, DecompositionType::NONE, nullptr, -1, nullptr, false, 0, 0, 0};
}

现在,当我尝试使用它时,我遇到了一个段错误,所以我开始使用 gdb 调试它并得到以下输出:

Breakpoint 1, nsucs::CodepointInfo::get (codepoint=0) at /home/richard/src/libnsucs/lib/codepoint_info.cc:21
21 uint32_t hash = fnv1a(codepoint) % NSUCS_CODEPOINTTABLE_NUM_BUCKETS;
(gdb) n
22 CodepointInfo **bucket = codepoint_table[hash];
(gdb) n
23 for(uint32_t i = 0; bucket[i] != nullptr; i++) {
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff78cbd44 in nsucs::CodepointInfo::get (codepoint=0) at /home/richard/src/libnsucs/lib/codepoint_info.cc:23
23 for(uint32_t i = 0; bucket[i] != nullptr; i++) {
(gdb) print hash
$1 = 18805
(gdb) print codepoint_table[hash]
$2 = (nsucs::CodepointInfo **) 0x7ffff7d61d00 <nsucs::codepoint_table_fragment_18805>
(gdb) print bucket
$3 = (nsucs::CodepointInfo **) 0x0

codepoint_table[hash]bucket 在将一个分配给另一个后不应该相等吗?当我用 codepoint_table[hash] 替换 bucket 的用法时,它仍然会出现段错误,但是 gdb 中的 print codepoint_table[hash][i] 会产生正确的结果结果。

这里发生了什么?二进制文件根本没有优化。

编辑:

CodepointInfo 结构的定义:

struct CodepointInfo {
static const CodepointInfo get(uint32_t codepoint);

uint32_t codepoint;
const char *name;
uint32_t general_category;
uint8_t canonical_combining_class;
BidiClass::Enum bidi_class;
DecompositionType::Enum decomposition_type;
uint32_t *decomposition_mapping;
int8_t decimal_value;
const char *numeric_value;
bool bidi_mirrored;
uint32_t simple_uppercase_mapping;
uint32_t simple_lowercase_mapping;
uint32_t simple_titlecase_mapping;
};

最佳答案

通过将 extern CodepointInfo ***codepoint_table; 更改为 extern CodepointInfo **codepoint_table[]; 来修复它,这与外部编译单元中的声明相匹配。

仍然不知道为什么这会导致错误的分配行为。

关于c++ - 将指针分配给指针并不能使它们相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com