昨天我发布了这个: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>
的输出吗? , 也?让我们知道是否(不是)这种情况。
我是一名优秀的程序员,十分优秀!