Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序在raspberry pi上运行不正常_C - Fatal编程技术网

C程序在raspberry pi上运行不正常

C程序在raspberry pi上运行不正常,c,C,我目前正在设计一个卷边代码。这段代码在我的计算机上运行得很好,但是当我把它移植到我的pi时,它就不能正常工作了。我不知道为什么,而且我对C和覆盆子pi还很陌生。任何帮助都将不胜感激 以下是我的完整代码: #include <stdio.h> #include <stdbool.h> #include <math.h> #include <string.h> #include <stdlib.h> int main(void){

我目前正在设计一个卷边代码。这段代码在我的计算机上运行得很好,但是当我把它移植到我的pi时,它就不能正常工作了。我不知道为什么,而且我对C和覆盆子pi还很陌生。任何帮助都将不胜感激

以下是我的完整代码:

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int main(void){
    int bitLen, errorLoc;

    printf("\nLength of the data bits: ");
    scanf("%d", &bitLen);

    char binStr[ bitLen ], binStrErr[ bitLen ];
    printf("Data stream without error: ");
    scanf("%s", &binStr);

    if(strlen(binStr) > bitLen || strlen(binStr) < bitLen)
    {
        printf("\nLength of data stream given does not match stated input length!");
        return 0;
    }

    printf("Location of data bit that has error: ");
    scanf("%d", &errorLoc);

    if(errorLoc > bitLen)
    {
        printf("\nValue given is bigger than the input length!");
        return 0;
    }

    //Number Of Check Bits Needed
    int rBit = 1;
    while (pow(2, rBit) < (bitLen + rBit + 1))
    {
        rBit = rBit + 1;
    }

    int checkBitsArr[rBit];
    int checkBitsErrArr[rBit]; 

    //Actual size of array
    bitLen = bitLen + rBit;

    int binNum[bitLen];
    int binNumErr[bitLen];
    int size = sizeof(binNum) / sizeof(binNum[0]);
    int binNumPos = size;

    printf("\nData stream: ");
    //Flipping the error bit and storing into another string
    printf("\nOriginal data stream: ");
    for (int i = 0; i < strlen(binStr); i++){
        printf("%c", binStr[i]);
        if(i == (strlen(binStr)) - errorLoc){
            int temp = ((binStr[i] - '0') == 0) ? 1 : 0;
            binStrErr[i] = temp + '0';
        }
        else{
            binStrErr[i] = binStr[i];
        }
    }


    printf("\nData stream with error: ");
    for (int i = 0; i < strlen(binStr); i++){
        printf("%c", binStrErr[i]);
    }

    //Filling in the bits into two arrays: One is the correct data stream and one with error
    for (int i = strlen(binStr); i >= 0; i--)
    {
        binNum[binNumPos] = binStr[i] - '0';
        binNumErr[binNumPos] = binStrErr[i] - '0';

        binNumPos--;
    }

    printf("\n\n");
    //Moving bits to left to make space
    int position = 1;
    for (int i = size - 1; i >= 0; i--)
    {
        if ((position & (position - 1)) == 0)
        {
            for (int c = 0; c <= i; c++)
            {
                binNum[c - 1] = binNum[c];
                binNumErr[c - 1] = binNumErr[c];
            }
            binNum[i] = 33;
            binNumErr[i] = 33;
        }
        position++;
    }

    //Settings check bits into place
    position = 1;
    int checkBitIndex = 0;
    for (int i = size - 1; i >= 0; i--)
    {
        //Get check bit position
        if ((position & (position - 1)) == 0)
        {
            int temp = 0;//number of 1s in relation to the check bit
            int tempErr = 0;

            int maxNum = (i - position) + 1;
            if (maxNum < 0)
                maxNum = maxNum + (-1 * maxNum);

            //first part of check
            while (maxNum < i)
            {
                if (binNum[maxNum] == 1)
                {
                    temp++;
                }
                if (binNumErr[maxNum] == 1)
                {
                    tempErr++;
                }
                maxNum++;
            }
            int startNum = (i - position) + 1;

            //If the start number is less than zero, make it zero
            if (startNum < 0)
                startNum = startNum + (-1 * startNum);

            //Skip check method. Get the next set of check values in relation to the current check bit
            for (int x = startNum - (position * 2); x >= 0; x = x - (position * 2))
            {
                int k = 0;
                while (k < position)
                {
                    if (binNum[x + k] == 1)
                    {
                        temp++;
                    }
                    if (binNumErr[x + k] == 1)
                    {
                        tempErr++;
                    }
                    k++;
                }
            }
            //Set the value of check bit
            binNum[i] = (temp % 2 == 0) ? 0 : 1;
            binNumErr[i] = (tempErr % 2 == 0) ? 0 : 1;

            //Replace the current value with the correct checkbit
            checkBitsArr[checkBitIndex] = binNum[i];
            checkBitsErrArr[checkBitIndex] = binNumErr[i];
            temp = 0;
            tempErr = 0;

            checkBitIndex++;
        }
        position++;
    }


    printf("\nSEC code: ");
    printf("\nOriginal data stream: ");
    for (int i = 0; i < size; i++)
    {
        printf("%d", binNum[i]);
    }
    printf("\nData stream with error: ");
    for (int i = 0; i < size; i++)
    {
        printf("%d", binNumErr[i]);
    }

    printf("\n\n");
    int checkIndex = (int)pow(2, rBit - 1);
    printf("\n\nCheckbits of data bits without error: \n");
    for (int i = checkBitIndex - 1; i >= 0; i--)
    {
        printf("C%d: %d   ", checkIndex, checkBitsArr[i]);
        checkIndex = checkIndex/2;
    }
    checkIndex = (int)pow(2, rBit - 1);
    printf("\n\nCheckbits of data bits with error: \n");
    for (int i = checkBitIndex - 1; i >= 0; i--)
    {
        printf("C%d: %d   ", checkIndex, checkBitsErrArr[i]);
        checkIndex = checkIndex/2;
    }
    checkIndex = (int)pow(2, rBit - 1);
    int posError = 0;
    printf("\n\nSyndrome code: \n");
    for (int i = checkBitIndex - 1; i >= 0; i--)
    {
        int x = checkBitsErrArr[i] ^ checkBitsArr[i];
        if(x == 1){
            posError += checkIndex;
        }
        printf("C%d: %d   ", checkIndex, x);
        checkIndex = checkIndex/2;
    }
    printf("\n\n");
    printf("\nPosition of error: %d\n\n", posError);
    // printf("\n\n");
    return 0;
}
以下是我在计算机和pi上的结果:

计算机结果(正确):

Pi结果(错误):


看起来您的问题远远不止一个,但让我们从第一个问题开始:

char binStr[ bitLen ], binStrErr[ bitLen ];
接下来请求的字符串不仅包含作为输入的16个字节,还包含一个额外的哨兵字符作为第17个字符

因此,在这一点上,您已经有了2个缓冲区溢出,您可以在Pi的输出中很好地看到这一点。在第一个示例中也会发生相同的缓冲区溢出,只是内存布局差异太大,因此不会产生可见的工件

for (int c = 0; c <= i; c++)
{
    binNum[c - 1] = binNum[c];
    binNumErr[c - 1] = binNumErr[c];
}
for(int c=0;c
for (int c = 0; c <= i; c++)
{
    binNum[c - 1] = binNum[c];
    binNumErr[c - 1] = binNumErr[c];
}