gpt4 book ai didi

c - 查询有关通过 valgrind 运行程序并与其他系统相比得到错误结果的问题

转载 作者:太空宇宙 更新时间:2023-11-04 04:07:05 25 4
gpt4 key购买 nike

昨天我发布了这个:What's the problem with this code? [hashtable in C] paxdiablo 主动提出要帮助我。他发布了一个代码示例,并要求我在我的机器上通过 valgrind 运行它。此代码通常生成:12,4 但在我的机器上,我得到 24,8。加倍了!我只是好奇为什么会这样。希望某人有一个很好的解释。

我还发布了 paxdiablo 的代码(对于找不到它的人。)

#include <stdio.h>
#include <stdlib.h>

typedef struct HashTable {
int size ;
struct List *head;
struct List *tail;
} HashTable;

typedef struct List {
char *number;
char *name;
int time;
struct List *next;
} List;

#define size_of_table 211

HashTable *createHashTable(void) {
HashTable *new_table = malloc(sizeof(*new_table)*size_of_table); //line 606
printf ("%d\n", sizeof(*new_table));
printf ("%d\n", sizeof(new_table));
if (new_table == NULL) {
return NULL;
}

int i=0;
for(i; i<size_of_table; i++) {
new_table[i].size=0;
new_table[i].head=NULL;
new_table[i].tail=NULL;
}
return new_table;
}

int main(void) {
HashTable *x = createHashTable();
free (x);
return 0;
}

最佳答案

你的环境指针是 8 字节宽,而你的 int从外观上看,s 也是 8 字节宽。从尝试使用 -m32 时的错误判断使用 gcc,我猜你有 x86_64已安装 glibc-devel 版本,需要额外安装 x86(32 位)版本。

你能发布 ldd <your_compiled_program> 的输出吗? , 也?让我们知道是否(不是)这种情况。

关于c - 查询有关通过 valgrind 运行程序并与其他系统相比得到错误结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661358/

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