【发布时间】:2020-05-17 15:05:23
【问题描述】:
我发送初始化代码数组的大小为 500,然后用整数填充它并将其发送到此 用数组值填充结构的函数。但是当试图访问code[0]时,机器崩溃了。
instruction *fetchCycle(int *code, instruction *ir, int pc)
{
int index = pc * 4;
printf("accessing code[%d]\n", index);
ir->op = code[index++];
printf("accessing code[%d]\n", index);
ir->r = code[index++];
printf("accessing code[%d]\n", index);
ir->l = code[index++];
printf("accessing code[%d]\n", index);
ir->m = code[index++];
printf("accessing code[%d]\n", index);
return ir;
}
这是调用fetchCycle()的函数
// takes in a single instruction and executes the command of that instruction
void executionCycle(int *code)
{
int l, m, sp = MAX_DATA_STACK_HEIGHT, bp = 0, pc = 0, gp = -1, halt = 0, i = 0;
int data_stack[41] = {0}, reg[200];
instruction *ir;
// Capturing instruction integers indicated by program counter
ir = fetchCycle(code, ir, pc++);
// printf("5\n");
while (halt == 0)
{
// printf("6\n");
switch(ir->op)
{ ...
这是终端的输出:
访问代码[0] 分段错误(核心转储)
【问题讨论】:
-
指令可能不是整数。您应该使用
unsigned char将它们作为内存中的字节访问。 -
请提供A Minimal, Complete, and Verifiable Example (MCVE)。
code在哪里声明和初始化?它只是一个未初始化的指针吗?还是指针已经超出了它指向的末尾? -- 这肯定可以解释 SegFault。
标签: c arrays pointers segmentation-fault integer