我在 C 中有以下结构。
struct a {
long state;
long uid;
long w, x, y, z, xx, yy, zz, xxx, yyy, zzz;
char comm[64];
};
然后我做malloc
如下。
buf = malloc (100 * sizeof(struct a));
但是,当我尝试按如下方式访问各个结构时,会出现段错误。
for (i = 0; i < 100; ++i) {
tmp = buf + (i * sizeof(struct a));
printf ("\t>%d>%ld,%ld\n", i, tmp->state, tmp->uid);
}
在前 10 个条目后,我遇到了段错误。我不知道为什么会这样。请帮忙。