while循环和数组导致异常行为...可能是内存混合 [英] While loops and arrays causing very odd behaviour...maybe a memory mixup

查看:113
本文介绍了while循环和数组导致异常行为...可能是内存混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对运行时发生的这种愚蠢行为感到厌倦,尽管我敢肯定,当我们的程序在运行时以最晦涩的方式搞砸时,我们所有人都是如此。

I'm tired of this tom-foolery occurring during runtime , although I'm sure we all are, when our programs screw up at runtime in the most obscure ways.

到目前为止,整个源代码在这里都有点大,但仍小于200行,因此此处。如果运行该程序,请使用它,因为我在下面发布的代码只是我认为错误所在的函数。

Getting to the point, the entire source code is a bit large to place here, but still <200 lines, so that's here . Use it if running the program, since the code I will post below is just functions, where I think the error lies.

上下文:这是一种

这个问题很奇怪。基本上, encrypt()函数始终可以正常工作-我通过在纸上自己为算法进行匹配;例如,当Pin为 12345678 时,ABC正确编码为 3c 45 46 -6f

The issue is strange. Basically, the encrypt() function works correctly always -I've matched it by doing the algorithm for myself on paper ; for example, ABC is correctly encoded to 3c 45 46 -6f when the Pin is 12345678.

decrypt()函数是一个奇怪的问题。

The strange issues are with the decrypt() function.

首次运行该程序时,尝试在有效密文-pin对始终上运行 decrypt(),除了 / n (换行符)。当尝试使用有效的不同密码-密文对时,首先成功运行 encrypt()之后, crypto()函数只返回已加密的同一条消息,或者从先前编码的消息中返回一些其他随机输出。

When the program is run for the first time, trying to run decrypt() on a valid ciphertext-pin pair always returns nothing except a /n (newline) . When tried with a different valid pin-ciphertext pair, after a successful run of encrypt() is done first, the decrypt() function just returns either the same message which was just encrypted or some other random output from the previously encoded message.

不用多说,传奇般的搞砸了现在我已经重建了三次的解密函数-

Without further ado, the legendarily screwed up decrypt function which I have rebuilt thrice now -

void decrypt()
{
    printf("\n");
    int *digits = pin(); int d[8];
    getchar();
    for (int i=0;i<8;i++)
        d[i] = *(digits + i); //puts each digit in a local array.

    printf("\nEnter encoded message -\n\n");
    getchar();
    
    int j; char ch, msg[3002];
    for(int i=0; i < 3000;i++)
    {
        scanf("%x",&j);

        if(j==-111){
            msg[i] = '\0'; //terminates string with \0
            break; 
        }
        else{
            if(ctln(i)==1)
                ch = j - d[2];
            else if(fib(i)==1)
                ch = j + d[4];
            else if(luc(i)==1)
                ch = j - d[0];
            else if(pent(i)==1)
                ch = j + d[6];
            else if(hex(i)==1)
                ch = j - d[3];
            else if(prm(i)==1)
                ch = j + d[7];
            else {
                if(i%2 == 0)
                    ch = j - d[1];
                else
                    ch = j + d[5];          
            msg[i] = ch;
            }
        }
    }
    printf("\nDecrypted message -\n\n");
    puts(msg);    
}

对于上下文,以及在这里找到罪魁祸首,请务必阅读完整的代码< a href = https://drive.google.com/file/d/1psLji6Z2RIcpAMcgCuwxQaoMvE1uZzND/view?usp=sharing rel = nofollow noreferrer>此处,其中 pin() 返回指向包含所有8位数字的静态整数数组的指针,以及 ctln() fib() luc() pent() hex() prm() [检查位置值消息中 char 的i 是加泰罗尼亚语,斐波那契,卢卡斯,五角大楼,六边形,素数系列的一部分。 此处

For context, as well as finding the culprits here, do make sure to read the full code here , with the pin() returning a pointer to a static int array holding all 8 digits , as well as the ctln() , fib(), luc(), pent(), hex(), prm() [ which check if position value i of char in message is a part of Catalan, Fibonacci , Lucas, Pentagon, Hexagon, Prime number series. More here.

编辑1

我已经尝试过保留不同的变量名,还有一些我无法完全回忆的东西。另外,由于它非常相关,因此下面是 pin()函数:

I have already tried keeping different variable names, and some other things I can't fully recall. Also, because it is very relevant, below is the pin() function:

int *pin() 
{
    
    int num,q=0; static int pins[8];

    
    printf("Enter 8-digit PIN : ");
    scanf("%d", &num);

    
    for(register int i = 10000000 ; i >= 1 ; i = (i/10)) // i is position of digit.
    {
        int d = ((num - (num % i)) / i); // d stores 'digit' ( divides quotient of (num % i) by i) 
        pins[q] = d; q++;
        num = (num - ( d * i )); 
    }
    

    return pins ; // pointer to static array storing digits of PIN 
} 

编辑2

我错误地分配了 pins [6] 而不是 pins [8] 在原始代码中,我已经更正了它,但是仍然遇到相同的错误。

I had wrongly assigned pins[6] rather than pins[8] in the original code, I have corrected it but am still facing the same errors.

编辑3

更正后由MikeCAT指出的错误,它现在在解密时会忽略第一个字符。

After correcting the mistake pointed out by MikeCAT, it now ignores the first character when deciphering.

编辑4

<$责怪 scanf()之前的c $ c> getchar(),删除它也解决了最后一个问题。谢谢@MikeCAT!

The getchar() before scanf() was to blame, removing it fixes the last issue too. Thanks @MikeCAT !

推荐答案

在您的 decrypt()函数中, msg [i] = ch; 仅在以下函数中执行: ctln,fib,luc,pent,hex,prm 返回 1

In your decrypt() function, msg[i] = ch; is executed only if none of the functions ctln, fib, luc, pent, hex, prm returned 1.

因此,非静态局部变量 msg 不确定,可用于打印,并可调用未定义的行为

Therefore, uninitialized value of non-static local variable msg, which is indeterminate, may be used for printing and undefined behavior may be invoked.

零件

            msg[i] = ch;
            }

应该是

            }
            msg[i] = ch;

,因为它是通过 encrypt()函数完成的。

as it is done in encrypt() function.

这篇关于while循环和数组导致异常行为...可能是内存混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆