【问题标题】:C: How to insert and read a string to/from a Judy HashC:如何在 Judy Hash 中插入和读取字符串
【发布时间】:2017-09-06 00:00:42
【问题描述】:

我一直在寻找答案,但我仍然不清楚我应该做什么。

在所有官方示例中,所有值和索引以及指向它们的指针都是 Word_t 或 PWord_t 类型,据我所知,这只是某种类型的 int。我的困惑是试图理解它如何引用 char* 或缓冲区(char[])或原始字符串(“string”)

这是我尝试过的东西,但输出难以理解(就像您在打印二进制文件时看到的那样)

#include <stdio.h>
#include <Judy.h>


int main()
{

        Pvoid_t   PJArray = (PWord_t)NULL;  // Judy array.
        PWord_t   PValue;                   // Judy array element.
        Word_t    Bytes;                    // size of JudySL array.

        JSLI(PValue, PJArray, "WHAT");
        *PValue = "HELLO...";

        JSLG(PValue, PJArray, "WHAT");
        printf("%s\n", &PValue);

        return 0;
} 

输出是:

`(@��

【问题讨论】:

  • 我不熟悉这里的 API,文档也很痛苦,但似乎如果您使用 *PValue = "HELLO..." 编写,您应该通过 %s*PValue 阅读和不是&amp;PValue

标签: c judy-array


【解决方案1】:

我通过我的工具运行了您的代码,它显示代码存在缓冲区溢出问题。这是错误消息:

DTS_MSG: Stensal DTS detected a runtime program error. Abort execution!
DTS_MSG: Reading 1 bytes at 0xbfffdba0 violates type safety.
DTS_MSG: Diagnostic information:

- The object to-be-read (start:0xbfffdb9c, size:4 bytes) is allocated at
-     file:/tmp/a.c::9, 19 
-  0xbfffdb9c               0xbfffdb9f
-  +------------------------+
-  | the object  to-be-read |......
-  +------------------------+
-                            ^~~~~~~~~~
-        the read starts at 0xbfffdba0 that is right after the object end.
- Stack trace (most recent call first):
-[1]  file:/src/string/memchr.c::25, 9 
-[2]  file:/src/stdio/vfprintf.c::602, 8 
-[3]  file:/src/stdio/vfprintf.c::678, 8 
-[4]  file:/src/stdio/printf.c::9, 8 
-[5]  file:/tmp/a.c::16, 9 
-[6]  file:/src/env/__libc_start_main.c::149, 11 

基本上,&PValue 获取 Pvalue 的地址,而 printf 将其视为 4 字节的 JudySL 数组的地址。如果你改变了

 printf("%s\n", &PValue);

 printf("%s\n", PValue);

它会为我打印出“P{WHAT”。我不熟悉 Judy hash,所以你需要验证它是否是预期的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多