【发布时间】:2010-03-10 19:26:55
【问题描述】:
出于某种原因,如果我尝试获取 mystruct 的实际大小,我会继续获取大小 1。
我知道mystruct 正在保存数据,因为我可以将其转储出来,并且所有内容都在mystruct 中。
获得尺寸 1 的原因可能是什么? 谢谢
// fragments of my code
struct mystruct {
char *raw;
int count;
};
struct counter {
int total; // = 30
}
static struct mystruct **proc()
{
int i = 0;
gchar *key,*val;
struct mystruct **a_struct;
struct counter c;
a_struct = (struct mystruct **)malloc(sizeof(struct mystruct *)*c.total);
while (table (&iter, (gpointer) &key, (gpointer) &val)) {
a_struct[i] = (struct mystruct *)malloc(sizeof(struct mystruct));
a_struct[i]->raw = (char*)key;
a_struct[i++]->count = (int)val;
}
size_t l = sizeof(a_struct) / sizeof(struct mystruct*);
printf("%d",l); // outputs 1
}
【问题讨论】:
-
另外,对 size_t 使用 %zu 而不是 %d 格式说明符。
-
谢谢,问题解决了!