我正在尝试在结构元素上使用 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) 的结构定义:
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;