简体   繁体   English

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

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

They're actually initialized to the character '0' (ie 48).它们实际上被初始化为字符'0'(即48)。 I'm making a logic simulator and each bit is represented as a single char, either '0' or '1'.我正在制作一个逻辑模拟器,每个位都表示为一个字符,“0”或“1”。 All of my other components have worked until I made an 8-bit selector.在我制作了一个 8 位选择器之前,我所有的其他组件都可以正常工作。
This is the code for the 1-bit selector.这是 1 位选择器的代码。 It works fine.它工作正常。

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;
}

The 8-bit selector is basically 8 1-bit selectors tied together. 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;
    }
}

However, when I test it with the following function, I get unexpected output.但是,当我用下面的 function 测试它时,我得到了意想不到的 output。

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);
}

Expected output:预期 output:

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

Actual output:实际 output:

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

Unless I'm reading my code wrong, all of those selectors should have their data initialized to "00".除非我读错了我的代码,否则所有这些选择器都应该将它们的数据初始化为“00”。 Why don't they?他们为什么不呢?

EDIT: I'm using the latest gcc on ARM if that helps.编辑:如果有帮助,我在 ARM 上使用最新的 gcc。

I was reading my code wrong (imagine that).我读错了我代码(想象一下)。 I switched up an i and aj (line 5 of the Selector8 function definition).我切换了 i 和 aj(Selector8 function 定义的第 5 行)。 The j was supposed to come before the i in this case.在这种情况下, j 应该在 i 之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM