c - Printf 以某种方式改变了一些东西?

标签 c debugging gcc printf

好的,所以我有以下 C 代码

#include <cstdio>
#include <cstring>

// funkcija za mnozenje na dva 8-bitni broja (vo RC format) so Butov algoritam
// vlez:    a[] - mnozenik, b[] - mnozitel
// izlez:   proizvod[] - proizvodot (mnozenik * mnozitel)


void shiftRight(char niza[])
{
    char out[100];
    strncpy(out, niza, 1);
    strcat(out, niza);
    out[17]='\0';
    strcpy(niza, out);
}


void add(char opa[], char opb[])
{
    char rez[100];
    strcpy(rez, opa);
    char carry='0';
    int i=16;
    while(i>=0)
    {
        int car=carry-'0';
        int currbita=opa[i]-'0';
        int currbitb=opb[i]-'0';
        rez[i]=((car+currbita+currbitb)%2)+'0';
        if(car+currbita+currbitb>=2)
        {
            carry='1';
        }
        else
            carry='0';
        i--;
    }
    strcpy(opa, rez);
}

void vtorKomplement(char in[], char out[])
{
    strcpy(out, in);
    for(int i=0; i<8; i++)
    {
        if(out[i]=='0')
            out[i]='1';
        else
            out[i]='0';
    }
    int i=7;
    char carry='1';
    while(carry!='0')
    {
        int car=carry-'0';
        int currbit=out[i]-'0';
        if(car+currbit>=2)
        {
            carry='1';
        }
        else
            carry='0';
        out[i]=((car+currbit)%2)+'0';
        i--;
    }
}

void mnozenjeButov(char a[], char b[], char proizvod[]) {
    int i;
    char rez[100];
    char A[100];
    char S[100];
    char P[100];
    strcpy(A, a);
    strcat(A, "000000000");
    vtorKomplement(a, S);
    for(i=8; i<17; i++)
    {
        S[i]='0';
    }
    S[17]='\0';
    strcpy(P, "00000000");
    strcat(P, b);
    strcat(P, "0");
    for(int i=0; i<8; i++)
    {
        if(P[15]=='0'&& P[16]=='1')
        {
            add(P, A);
        }
        else if(P[15]=='1' && P[16]=='0')
        {
            printf("Before add P: %s\n", P);
            add(P, S);
        }
        shiftRight(P);
        printf("Shifted P: %s\n", P);
    }
    for(int i=8; i<17; i++)
    {
        proizvod[i-8]=P[i];
    }
    proizvod[8]='\0';
}

int main() {
    int success = 1;

    char a[100];
    char b[100];
    char proizvod[100];
    char w_proizvod[100];

    // TEST 1
    strcpy(a, "00010011");
    strcpy(b, "00000101");
    strcpy(w_proizvod, "01011111");
    mnozenjeButov(a, b, proizvod);
    printf("TEST 1: %s, %s\n", a, b);
    printf("  Tocen odgovor:    %s\n", w_proizvod);
    printf("  Vas odgovor:      %s\n", proizvod);

    if (strcmp(proizvod, w_proizvod) == 0) {
        printf("Vasata programa dava tocen rezultat :-)\n\n");
    } else {
        printf("Vasata programa dava netocen rezultat!\n\n");
        success = 0;
    }

    if (success == 1) {
        printf("Vasata programa gi pomina testovite uspesno!\n");
    } else {
        printf("Nekoi od testovite bea neuspesni.\n");
    }

    return 0;
}

一切都很好,但是当我删除 printf("Before add P: %s\n", P); 和/或之后的 printf 时,会发生一些奇怪的事情。然后输出以某种方式发生变化,并且出现一些不应该出现的字符...我尝试调试,但后来得到了正常的输出。我还尝试在另一台机器上进行测试,我也在那里得到了奇怪的字符。过去一个小时我一直在敲头,有人能告诉我哪里出错了吗?我正在将代码块与 mingw GCC 编译器一起使用。

更新: Jens Gustedt 的解决方案奏效了。

最佳答案

这两行存在概念错误:

strncpy(out, niza, 1);
strcat(out, niza);

strncpy 这里只复制一个字符。特别是 out[0] 等于 niza[0] 并且 out[1] 是之前存在的任何内容。然后,您的 strcatniza 写入找到 0 字符的下一个位置,这可能会产生灾难性的结果。 (strncpy 的手册页说得很好。)

为了能够在之后执行strcpy,您可能必须在其中放置一个'\0'。但有一个更简单的解决方案:

out[0] = niza[0];
strcpy(out + 1, niza);

关于c - Printf 以某种方式改变了一些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153751/

相关文章:

c - Visual Studio 2013 不会编译 C 数组声明

c - c中如何控制栈的大小

python - 倒下的多米诺骨牌,调试

c# - 使用附加到进程在 Visual Studio 2008 中调试 DLL 项目

c++ - 在 Windows 上编译 gcc 4.7

c - int32_t : gcc/linux 86 vs amd64 的对齐要求

C 链接错误 : undefined reference to 'main'

c - 我不知道在 C 中使用冒号

c - 从文件读取并传递给C中的二维数组

c# - Visual Studio调试器在混合 Debug模式下停止击中断点