将指针分配给指针不会使它们相等
Assigning pointer to pointer does not make them equal
我有一个unicode代码点描述的大型静态哈希图。每个哈希值都会导致指向元素的指针数组以null结尾。以下是我的访问功能:
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};
}
现在,当我尝试使用它时,我遇到了一个segfault,所以我开始用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++将一个指针分配给另一个指针时执行的类型检查
- 我似乎无法为指针分配一个数组,然后更改数组的内容
- C++为 void 指针分配一个字符串
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- MPI 集合通信中的指针分配
- 函数指针分配在创建 DLL 后导致错误
- 通过双指针分配指针
- C++编译时使用 constexpr 字符数组指针分配静态数组?
- 我可以直接为指针分配地址吗?如果是,如何做到这一点
- 为什么为char指针分配内存不会截断char序列
- C++指针分配字符数组的地址
- 将 void 指针分配给另一个 void 指针
- 如何将不同的成员函数指针分配给注册类的不同实例?
- 如何为指针分配特定的内存地址?
- C++ - 无法将顶部常量指针分配给另一个非常量指针
- 将指针分配给另一个指针时会发生什么情况?
- 为指向C++中的对象/对象的指针分配堆内存
- 将指针分配给字符串变量中包含的地址
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用