C/Digital 逻辑 - 为什么我的零初始化变量会改变值?

C/Digital Logic - Why are my zero-initialized variables changing value?

它们实际上被初始化为字符'0'(即48)。我正在制作一个逻辑模拟器,每个位都表示为一个字符,“0”或“1”。在我制作 8 位选择器之前,我的所有其他组件都可以正常工作。
这是 1 位选择器的代码。它工作正常。

typedef char Bit;
...
typedef struct SelectorComp {
    Bit store;
    Bit data[2];
    NandGate nand[3];
    NotGate not;
    Bit out;
} SelectorComp;

void initSelector(SelectorComp* selector) {
    int i;
    selector->store = '0';
    for (i = 0; i < 2; i++) selector->data[i] = '0';
    initNot(&selector->not);
    for (i = 0; i < 3; i++) initNand(&selector->nand[i]);
    Selector(selector);
}

void Selector(SelectorComp* selector) {
    selector->nand[0].in[0] = selector->store;
    selector->nand[0].in[1] = selector->data[0];
    Nand(&selector->nand[0]);
    selector->not.in = selector->store;
    Not(&selector->not);
    selector->nand[1].in[0] = selector->not.out;
    selector->nand[1].in[1] = selector->data[1];
    Nand(&selector->nand[1]);
    selector->nand[2].in[0] = selector->nand[0].out;
    selector->nand[2].in[1] = selector->nand[1].out;
    Nand(&selector->nand[2]);
    selector->out = selector->nand[2].out;
}

8 位选择器基本上是 8 个 1 位选择器捆绑在一起。

typedef struct Selector8Comp {
    Bit store;
    Bit data[2][8];
    SelectorComp selector[8];
    Bit out[8];
} Selector8Comp;

void initSelector8(Selector8Comp* selector8) {
  int i, j;
    selector8->store = '0';
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 8; j++) {
            selector8->data[i][j] = '0';
        }
    }
    for (i = 0; i < 8; i++) initSelector(&selector8->selector[i]);
    Selector8(selector8);
}

void Selector8(Selector8Comp* selector8) {
    int i, j;
    for (i = 0; i < 8; i++) {
        selector8->selector[i].store = selector8->store;
        for (j = 0; j < 2; j++) selector8->selector[i].data[j] = selector8->data[i][j];
        Selector(&selector8->selector[i]);
        selector8->out[i] = selector8->selector[i].out;
    }
}

但是,当我用下面的函数测试它时,我得到了意外的输出。

void testSelector8() {
    int i;
    Selector8Comp* selector8 = (Selector8Comp*)malloc(sizeof(Selector8Comp));
    initSelector8(selector8);

    printf("Testing 8-bit selector\n");

    printf("store: %c\n", selector8->store);
    printf("byte 0: ");
    for (i = 0; i < 8; i++) printf("%c", selector8->data[0][i]);
    printf("\nbyte 1: ");
    for (i = 0; i < 8; i++) printf("%c", selector8->data[1][i]);
    printf("\nselectors:\n");
    for (i = 0; i < 8; i++) {
        printf("%d: store: %c in: %c%c out: %c\n", i, selector8->selector[i].store, selector8->selector[i].data[0], selector8->selector[i].data[1], selector8->selector[i].out);
    }
    printf("\noutput: ");
    for (i = 0; i < 8; i++) printf("%c", selector8->out[i]);
    printf("\n");

    free(selector8);
}

预期输出:

store: 0
byte 0: 00000000
byte 1: 00000000
selectors:
0: store: 0 in: 00 out: 0
1: store: 0 in: 00 out: 0
2: store: 0 in: 00 out: 0
3: store: 0 in: 00 out: 0
4: store: 0 in: 00 out: 0
5: store: 0 in: 00 out: 0
6: store: 0 in: 00 out: 0
7: store: 0 in: 00 out: 0

output: 0000000

实际输出:

store: 0
byte 0: 00000000
byte 1: 00000000
selectors:
0: store: 0 in: 00 out: 0
1: store: 0 in: 00 out: 0
2: store: 0 in: 00 out: 0
3: store: 0 in: 11 out: 1
4: store: 0 in: 10 out: 0
5: store: 0 in: 10 out: 0
6: store: 0 in: 01 out: 1
7: store: 0 in: 01 out: 1

output: 00010011

除非我读错了我的代码,否则所有这些选择器的数据都应该初始化为“00”。他们为什么不呢?

编辑: 如果有帮助,我正在 ARM 上使用最新的 gcc。

读错了我的代码(想象一下)。我切换了 i 和 j(Selector8 函数定义的第 5 行)。在这种情况下,j 应该在 i 之前。