【问题标题】:Weird Behaviour of strcmp [C]strcmp 的奇怪行为 [C]
【发布时间】:2015-10-16 02:26:41
【问题描述】:

我做错了吗?我正在尝试将 test 和 msg 与 strcmp 进行比较。它们完全相同,但 strcmp 返回负值。

我正在尝试编写速记聊天。

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


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

   // exit(0);


    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[1], "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(const char * argv[], int argc) {

    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;

    char msg[max_msg_size];
    FILE *img_in = NULL;

    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);

    char* output = msg;

  //  printf("%s\n",output);

    return output;
}




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

    char message[1000];


  /*  printf("\n Send : ");

    fgets(message, 1000, stdin);
    message[strlen(message) -1] = '\0';


    encode(message, argv, argc);
    decode(argv, argc); */


   while (1) {

       printf("\n Send : ");

        fgets(message, 1000, stdin);


        encode(message, argv, argc);

       char *test;
       char *msg;

       msg = message;
       test = decode(argv, argc);


       long sizem = strlen(msg);
       long sizet = strlen(test);

       printf("size of message : \t %ld\n", sizem);
       printf("size of test : \t %ld\n", sizet);
       printf("Test = \t %s\n", test);
       printf("Message = \t %s\n", msg);


       long debug = strcmp(test, msg);
       printf("strcmp = \t%ld\n", debug);


      /* sizem = strlen(message);
       sizet = strlen(test);

       printf("%ld\n", sizem);
       printf("%ld\n", sizet);
       printf("%s\n", test);
       printf("%s\n", message); */

       // printf("%ld", debeug);

        exit(1);

        while (strcmp(test, message) == 1) {

        }


    }



    return 0;
}

Debug 1 Debug 2

输出:

Send : test
size of message :    5
size of test :   5
Test =   test

Message =    test

strcmp =    -116
Program ended with exit code: 1

感谢您的帮助!

【问题讨论】:

  • 请在此处以文本形式发布代码,而不是屏幕截图!
  • 应该有足够的代码以及示例输入/输出,以便有人能够复制您的问题,即Minimal, Complete, Verifiable Example
  • decode 正在返回一个局部变量,该变量在函数退出时超出范围。那是未定义的行为。
  • @kaylum:更准确地说,它返回一个指针,指向一个具有自动存储持续时间的对象。
  • @EricSchaal:这并不能解决任何问题。它只是泄露了你的分配。 相反,您可以使用char *output = strdup(msg); return output;。

标签: c arrays string debugging strcmp


【解决方案1】:

decode 函数返回一个指向 decode 函数本地数组的指针。

局部变量在函数返回时被销毁,所以这是一个悬空指针。在main 中使用它会导致未定义的行为。您所看到的可能是由于 strcmp 函数或任何其他原因将该数组用于其他目的的内存。

要使您的代码正常工作,您必须更仔细地考虑为要存储的解码字符串分配内存的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2019-04-10
    • 2019-05-24
    • 2016-07-15
    • 2011-06-23
    相关资源
    最近更新 更多