【发布时间】:2012-03-08 00:12:46
【问题描述】:
我正在尝试在这样的结构元素上使用 memset:
memset( &targs[i]->cs, 0, sizeof( xcpu ) );
但是,这样做会给我一个分段错误。我既不明白为什么会失败,也不明白如何让它发挥作用。 在结构的元素上使用 memset 的正确方法是什么,为什么我的方法不起作用?
为目标分配内存的行:
eargs **targs = (eargs **) malloc(p * sizeof(eargs *));
struct element cs (xcpu_context) 和 struct targs (execute_args) 的struct 定义:
typedef struct xcpu_context {
unsigned char *memory;
unsigned short regs[X_MAX_REGS];
unsigned short pc;
unsigned short state;
unsigned short itr;
unsigned short id;
unsigned short num;
} xcpu;
typedef struct execute_args {
int ticks;
int quantum;
xcpu cs;
} eargs;
【问题讨论】:
-
不要在 C 程序中转换来自
malloc()的返回值。 -
初始化 *targs 的代码在哪里?
-
@HansPassant - 它不存在,大概就是这个问题。
-
是的,这正是问题所在。不知道我是如何忘记这样做的,以及我在检查我的代码时是如何错过它的。
-
定义 != 声明 :)