访问大型数组的元素

时间:2018-05-27 04:06:38

标签: c struct typedef

我在访问某个阵列的元素时遇到问题。我事先为长代码道歉,但有必要充分解释这个问题。我有一个结构,我在头文件中声明如下:

typedef struct {
    // DC the current value of the Program Counter register
    unsigned short int DC;

    // PSR : Processor Status Register, bit[0] = P, bit[1] = Z, bit[2] = N, bit[15] = privilege bit
    unsigned short int GSR;

    //Registers - 8
    unsigned short int R[8];

    //Control signals 
    ControlSignals control_signals;

    //Memory 
    unsigned short int mem[65536];
} MachineState;

在链接器文件中,我正在读取二进制文件并将16位十六进制值存储到memory struct数组中。以下是该操作的相关设置:

//Rectify endianness
        if ((foo_array[0]) == 0xDECA || (foo_array[0]) == 0xB7C3 || (foo_array[0]) == 0x7EF1 || (foo_array[0]) == 0x5E71)
        {
            //Iterate through each element in array, swapping bytes to convert to big endian
            for (i = 0; i < bytes; i++)
            {
                //Swap bytes
                (foo_array[i]) = ((foo_array[i])>>8) | ((foo_array[i])<<8);
            }
        }

        //Iterate through temp array selecting only CADE/DADA instructions to copy to struct array
        for (i = 0; i < bytes; i++)
        {
            if ((foo_array[i]) == 0xCADE || (foo_array[i]) == 0xDADA)
            {

                //Increment to starting address
                i++;

                //Store start address
                start_address = foo_array[i];

                //Increment to n-body word specification
                i++;

                //Find number of words in relevant instructions
                foo = foo_array[i];

                //Increment to beginning of instruction
                i++;

                //Iterate through temp array selecting only CADE/DADA instructions to copy to struct array
                for (count = 0, j = start_address; count < foo; i++, j++, count++)
                {

                    //Assign values from temp array to machine memory array
                    machine->mem[j] = foo_array[i];
                    printf("address %05d in memory is: 0x%04X\n",j, machine->mem[j] );                          
                }

                //Once loop exited, decrement index variable to appropriate values
                if (count == foo)
                {
                    i--;
                }
            }   
        }

正如您可能想象的那样,在输出到机器内存中,会有十六进制值,有些为零,非零如下:

address 32805 in memory is: 0x0201
address 32806 in memory is: 0x0000
address 32807 in memory is: 0x8000
address 32808 in memory is: 0x0000
address 33280 in memory is: 0x9E00
address 33281 in memory is: 0x8000
address 40960 in memory is: 0x0001

我存储在数组中的值需要传递给要操作的函数,但我无法找到一种方法来传递所有那些填充了数组的值。数组很长,从索引0x0000到0x10000,但我想要传递所有值的唯一方法是在for循环中,选择当我点击数组中的第一个非零元素时,但问题是零值也非常重要,也必须使用。到目前为止,这是我对循环的实现:

 //Iterate through each element passing populated elements into function
        for (i = 0; i < (sizeof(machine.mem)) / (sizeof(unsigned short int)); i++)
        {
          //  printf("index is %d\n",i );
            if (machine.mem[i] != 0)
            {        
                printf("machine memory is: 0x%04X\n",machine.mem[i] );

                //Call UpdateMachineState to begin decoding machine instructions and execute one LC4 datapath
               //UpdateMachineState(&machine);   
            }
        } 

它允许我只找到非零值,但是如果数组中的起始数字或中间值为零怎么办?他们将完全错过。有没有办法获得所有填充值?我似乎无法弄清楚这一点。

编辑: 从收到的评论看来,可能不清楚实际问题是什么,所以我会尽力澄清。加载到机器内存后,其内容如下:

address 00000 in memory is: 0xF020
address 00001 in memory is: 0x9A00
address 00002 in memory is: 0xDBA0
address 00003 in memory is: 0x7B40
address 00004 in memory is: 0xF0FF
address 32800 in memory is: 0x9600
address 32801 in memory is: 0xD7A0
address 32802 in memory is: 0x9802
address 32803 in memory is: 0x78C0
address 32804 in memory is: 0x64C0
address 32805 in memory is: 0x0201
address 32806 in memory is: 0x0000
address 32807 in memory is: 0x8000
address 32808 in memory is: 0x0000
address 33280 in memory is: 0x9E00
address 33281 in memory is: 0x8000
address 40960 in memory is: 0x0001

这里索引00000是起始地址,索引40960是结束地址。问题是我不知道如何设置for循环,以便我可以将已加载到machine.mem结构中的所有内容传递到函数UpdateMachineState(&machine);中,如果有包含的元素包含值0x0000(例如地址32806和32808)。当声明struct machine.mem时,它的所有值都自动设置为零,所以我不确定如何区分预先设置的0x0000值和那里加载的值:machine->mem[j] = foo_array[i]。如有必要,我可以发布完整的代码。

1 个答案:

答案 0 :(得分:1)

  

声明struct machine.mem时,其所有值均为   自动设置为零,所以我不确定如何区分   预设的0x0000值以及在此处加载的值:   machine-> mem [j] = foo_array [i]。

您可以创建一个包含带有内存值的标志的结构,并在分配内存值时设置该标志。

赞:

typedef struct{
    unsigned short int value;
    bool value_assigned;
}mem_t;

然后,当您声明MachineState结构时,只需将mem声明为这些结构的数组即可,如下所示:

typedef struct {
    // DC the current value of the Program Counter register
    unsigned short int DC;

    // PSR : Processor Status Register, bit[0] = P, bit[1] = Z, bit[2] = N, bit[15] = privilege bit
    unsigned short int GSR;

    //Registers - 8
    unsigned short int R[8];

    //Control signals 
    ControlSignals control_signals;

    //Memory 
    mem_t mem[65536];
} MachineState;

然后在您要分配内存值的代码部分中,只需确保设置该标志即可。

//Iterate through temp array selecting only CADE/DADA instructions to copy to struct array
for (count = 0, j = start_address; count < foo; i++, j++, count++)
{

    //Assign values from temp array to machine memory array
    machine->mem[j].value = foo_array[i];
    machine->mem[j].value_assigned = true;
    printf("address %05d in memory is: 0x%04X\n",j, machine->mem[j]);

}

现在,您应该可以遍历该数组并检查value_assigned == true