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

问题描述 投票:0回答:1

它们实际上已初始化为字符“ 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”。他们为什么不呢?

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

c variables digital-logic
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.