malloc问题导致变量被覆盖

时间:2014-04-01 17:41:31

标签: c malloc

我的程序中有3个全局变量。出于某种原因,当我在string2bin上运行pad时,binaryMessage的内容会被覆盖,我无法弄清楚原因。我增加了malloc的尺寸,但这并没有帮助。我做错了什么?

请注意,randomPad()会创建一个随机的字符串" hex"值,应转换为表示十六进制字符串的二进制字符串。

char * temp;
char * binaryMessage;
char * pad;

void process_message(char *s)

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

        temp = (char *)malloc(sizeof(char *) *2048);

        binaryMessage = (char *)malloc(sizeof(char *) *2048);

        pad = (char *)malloc(sizeof(char *) * 2048);

        process_message("test");

}


char * char2bin ( unsigned char c ){
      static char bin[CHAR_BIT + 1] = {0};
      int i;

       for ( i = CHAR_BIT - 1; i >= 0; i-- ) {
            bin[i] = (c % 2) + '0';
            c /= 2;
       }

       return bin;
}

char* string2bin(char* str){
       int i;
       int len = strlen(str);

       sprintf(temp,"");
       for(i=0; i< len;i++){
             sprintf(temp, "%s%s",temp,char2bin(str[i]));
       }
       return temp;
 }

 char* randomPad(){
       int i;
       const char *hex_digits = "0123456789ABCDEF";
       char * p = (char*)malloc(sizeof(char*)*242);

       // clear old pad data
       //sprintf(pad,"");

       // create random string of 242 hex chars to use as pad
       for(i = 0; i< 242; i++){
             sprintf(p,"%s%c",p,hex_digits[ ( rand() % 16 ) ]);
       }
       return p;
 }
 void process_message(char *s){
       binaryMessage = string2bin(s);
       printf("m %s\nbm %s\n",s, binaryMessage);

       //get random one time pad
       pad = string2bin(randomPad());

       printf("m %s\nbm %s\n",s, binaryMessage);
 }

我的预期输出是:

m test
bm 01110100011001010111001101110100
m test
bm 01110100011001010111001101110100

我的实际输出是:

m test
bm 01110100011001010111001101110100
m test
bm 0011011100110001001110010100000100110010001110000011100001000101001100110100010000111000001101010100001100110010001100110011011100110111001100010011100000110100010001100011000101000100001101010011100000110101010000100011011000111000001101110011100100110010001110000011001100110010001100010011001101000100001100110011010101000110001100000011000100111000010001000011000101000011010000100011001000110101001110010011000100110100001110000011010000110010010000110011010001000001001101110100000101000001001101010011100000111001001101010011000100110000001101000011010101000001001101010100001001000100001110010011011000110111001110000011000100110101001100110100010101000100001101010011010100110011001110010011010001000010010000010100001100110011001100010011001000110000010001010100001100110001001101100011100001000100001100010100011000110100001100010100010100111001010001000011011000110000001110010011011100110001010001100011001101000001010001010100011000110000010000110011010000110101010000010011100001000010001100000011100100110010010001010011001000110110001101010011001001000011010001100100010001000110001110000011011101000100010001010100011000110010001100010011000001000101001110000011011100110111010001010011010000110010001101110011011101000101001101110011000100111000001110010100010101000010001100100011010100110010001101100011011001000011001101000100010100110110010000010011001000110011001100100011010000110110001101100011011101000110001100110011100100110001001110000011001101000110001100010011100000110101010001100011010000110000001100000011011101000010001110010100010101000001010000100011011100111000010000110100011000110010010000110100001100110111010001100011000101000100001101010011000100110011001100010100000101000101001100110011000000110001001110010011000100110111010000110100001000111000010001100100011000110010001100110100001101000100001110000011000001000101001110000100001001000100010001100100011000110001001100010011000001000110

2 个答案:

答案 0 :(得分:3)

这些界限至关重要:

sprintf(temp, "%s%s", temp, char2bin(str[i]));

...

sprintf(p, "%s%c", p, hex_digits[ ( rand() % 16 ) ]);

来自sprintf()'s man page

  

某些程序非常依赖于以下代码

       sprintf(buf, "%s some further text", buf);
  

将文字附加到buf。但是,标准明确指出,如果在调用 sprintf() snprintf() vsprintf(),和          的 vsnprintf()即可。根据所使用的gcc(1)的版本以及所使用的编译器选项,上述调用将产生预期结果。

答案 1 :(得分:1)

除了指出的错误之外,还有四个问题。

1-这:

char * p = (char*)malloc(sizeof(char*)*242);

应该是这样的:

char * p = malloc(sizeof(char)*243);  // Do NOT cast malloc

2-这:

sprintf(p,"%s%c", p, hex_digits[ ( rand() % 16 ) ]);

应该是这样的:

sprintf(p+i,"%c",hex_digits[ ( rand() % 16 ) ]);

3-你错过了这个:

*(p+242) = '\0';
return p;

4-最后,这行代码:

binaryMessage = string2bin(s);

binaryMessage指针值替换temp指针。

我能提供最好的建议吗? 摆脱所有全局变量。你需要为随机数生成器播种不可预知的东西(例如当前时间),否则你每次都会获得相同的“随机”数字序列。 / p>