簡體   English   中英

我算法的怪異行為

[英]Weird behaviour of my algorithm

我不明白為什么在無限循環內打印時會打印出正確的結果,但是在無循環時打印時卻什么也沒打印。

這是我的代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>


char msg[1000];


time_t get_mtime(const char *path)
{
    struct stat statbuf;
    if (stat(path, &statbuf) == -1) {
        perror(path);
        exit(1);
    }
    return statbuf.st_mtime;
}


void encode(char* msg, char * argv[], int argc)  {

    const int bmp_header_size = 54;
    int read_code;
    int msg_idx = 0;
    int img_idx = 0;
    int bit_idx = 0;
    char c;
    FILE *img_in = NULL;
    FILE *img_out = NULL;

    if( argc < 3 ){
        printf( "Usage: %s source_bmp output_bmp message.\n", argv[0] );
        exit(1);
    }

    img_in = fopen( argv[3], "rb" );
    if( img_in == NULL ){
        printf( "Could not open the input image file.\n" );
        exit(1);
    }

    img_out = fopen( argv[2], "wb" );
    if( img_out == NULL ){
        printf( "Could not open the output file.\n" );
        exit(1);
    }



    while( ( read_code = fgetc( img_in )) != EOF ){
        c = (char)read_code;

        if( img_idx >= bmp_header_size && msg_idx <= strlen( msg ) ){
            char bit_mask = 1 << bit_idx;

            if( ( msg[msg_idx] & bit_mask) > 0 )
                c |= 1;
            else
                c &= 254;

            bit_idx++;

            if( bit_idx >= 8 ){
                bit_idx = 0;
                msg_idx++;
            }           
        }

        fputc( c, img_out );
        img_idx++;
    }


    fclose(img_in);
    fclose(img_out);

}



char* decode(char * argv[], int argc) {

   /* printf("1 = %s\n", argv[1]);
     printf("2 = %s\n", argv[2]);
     printf("3 = %s\n", argv[3]); */

    const int bmp_header_size = 54;
    const int max_msg_size = 1000;

    int i;
    int c;
    int img_idx = 0;
    int msg_idx = 0;
    int bit_idx = 0;

    FILE *img_in = NULL;

    // char msg[max_msg_size];
    img_in = fopen( argv[2], "rb" );

    if( img_in == NULL ){
        printf( "Could not open the input image file.\n" );
        exit(1);
    }

    for( i=0; i < max_msg_size; i++ )
        msg[i] = 0;

    while( ( c = fgetc( img_in )) != EOF ){
        if( img_idx >= bmp_header_size ){
            char bit_mask = 0;
            if( ( c & 1 )  > 0 )
                bit_mask |= 1 << bit_idx;

            msg[msg_idx] |= bit_mask;

            bit_idx++;

            if( bit_idx >= 8 ){
                if( msg[msg_idx] == '\0' )
                    break;
                bit_idx = 0;
                msg_idx++;
            }
        }

        img_idx++;
    }

    fclose(img_in);
    return msg;

}




int main(int argc, char * argv[]) {

    char message[1000];






    while (1) {


        printf("\n Send : ");
        fgets(message, 1000, stdin);
        encode(message, argv, argc);

        long mod = get_mtime(argv[2]);
        long new = mod;

        while (1) {

            new = get_mtime(argv[2]);

            if (mod != new) {
                break;
            }
        }
        while (1) {
            printf("Received : %s ",decode(argv, argc));
        }


    }

    return 0;
}

讓我們關注一下(在main()中接近結尾處)

        while (1) {

            strcpy(received, decodeReverse(argv, argc));
            printf("Received : %s ",received);
        }

與上面的代碼我得到正確的輸出:

 Received : Hey
 Received : Hey
 Received : Hey
 Received : Hey
 Received : Hey

但是,如果received的打印沒有無限的while循環,我會得到:

Received : 

好像received是空的。

編輯:

我清理了代碼並應用了修復程序,但是如果沒有無限循環,它仍然無法正常工作(即使經過幾次迭代也是如此)。

感謝大伙們 !

返回的緩沖區被覆蓋。

char msg[max_msg_size];

僅存在於函數中,但仍被返回。

編譯器會在內存中為循環跳動,這意味着它可以幸存,但可能不在其他編譯器/平台上

char msg[max_msg_size];

是函數decode的局部變量,但是您嘗試返回它。

它與無限循環一起工作的事實與編譯器如何解釋該循環有關,但這是未定義的行為。

要解決此問題,請在decode功能之外聲明msg

請記住,您當然也需要在函數外部設置charg並將其返回之前,首先在函數外部執行char* output = msg delcare行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM