gpt4 book ai didi

c - malloc 问题导致变量被覆盖

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:59 26 4
gpt4 key购买 nike

我的程序中有 3 个全局变量。出于某种原因,当我在 pad 上运行 string2bin 时,binaryMessage 的内容被覆盖了,我不知道为什么。我增加了我的 malloc 的大小,但这没有帮助。我做错了什么?

请注意,randomPad() 创建一串随机“十六进制”值,应将其转换为表示十六进制字符串的二进制字符串。

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

最佳答案

这些行很关键:

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

...

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

来自 sprintf()'s man page :

Some programs imprudently rely on code such as the following

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

to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results.

关于c - malloc 问题导致变量被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com