我试图测试xxhash函数。所以我编写了一个简单的程序来生成 UUID 并将其传递给XXH32
函数。这是我的程序:
#define UUID_LEN 36
int main(int argc, char **argv)
{
uuid_t id;
char cid[UUID_LEN + 1];
int hash_count = atoi(argv[1]);
unsigned int hash[hash_count];
for(int i = 0;i < hash_count;i++) {
uuid_generate_random(id);
uuid_unparse(id, cid);
hash[i] = XXH32(cid, UUID_LEN, time(NULL));
uuid_clear(id);
}
return 0;
}
我将哈希生成的数量作为参数(./main 100
)传递。程序运行良好,但当我超过 100 万时它显示:Segmentation fault (core dumped)
. 我安装了所有调试信息包。当我用它打开核心文件gdb
时显示:
Reading symbols from /home/m.azimi/projects/testtommy/main...done.
[New LWP 8117]
Core was generated by `./main 10000000'.
Program terminated with signal 11, Segmentation fault.
#0 0x00000000004008e0 in main (argc=2, argv=0x7fffe19da598) at main.c:19
19 uuid_generate_random(id);
我试过了uuid_generate
,但没有帮助。我使用带有完全更新包的 fedora 19。您可以从这里下载核心文件。
[更新]
根据@Tarik 评论,我更改hash[i % 10000] = XXH32(cid, UUID_LEN, time(NULL));
为现在工作正常。另外我只保存生成的整数哈希。所以内存使用量为 10,000,000 * 4 字节 = 40MB。接下来我将程序更改为:
int main(int argc, char **argv)
{
/* allocate 10 million 4byte chunk = 40MB */
unsigned int hash[10000000];
return 0;
}
这也与核心崩溃:
[New LWP 13593]
Core was generated by `./main'.
Program terminated with signal 11, Segmentation fault.
#0 0x000000000040068b in main (argc=<error reading variable: Cannot access memory at address 0x7fff244a388c>,
argv=<error reading variable: Cannot access memory at address 0x7fff244a3880>) at main.c:9
9 {
新的核心文件可以从这里下载。为什么会发生这种情况?有操作系统级别限制吗?这是从堆栈而不是堆分配内存吗?因为unsigned int *hash = (unsigned int *) malloc(sizeof(unsigned int) * 10000000);
工作没有问题。