c - 我的算法的奇怪行为

标签 c function pointers printf

我不明白为什么当我在无限循环内打印时会打印出正确的结果,但当我在没有循环的情况下打印时却不会打印任何内容。

这是我的代码:

#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

但是如果 print received 没有无限 while 循环,我得到:

Received : 

就好像收到的是空的。

编辑:

我清理了代码并应用了修复程序,但在没有无限循环的情况下它仍然无法工作(即使经过几次迭代也无法正常工作)。

谢谢大家!

最佳答案

返回的缓冲区正在被覆盖。

char msg[max_msg_size];

仅存在于函数内,但正在返回。

编译器围绕循环的内存进行夹具,这意味着它恰好可以生存,但可能不会在不同的编译器/平台上

关于c - 我的算法的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33164804/

相关文章:

c - 在一个 block 中为不同类型分配内存

iphone - iphone、黑莓、安卓、诺基亚上的 Gstreamer?它的表现如何?

jquery - 如何仅使用 url 触发 jquery 函数?

C中4字节内循环移位28位

c - 同时运行的自旋锁

c - malloc 灾难性地失败

javascript - 给div添加单独用JS递增的文本

javascript - 如何使用javascript在一段时间后将CSS显示从可见更改为不可见

c++ - 在堆栈类中使用指针?

c# - C#中跨模块访问链表