下面給出的表現是我的示例代碼:的memcpy以意想不到的方式
int function1(unsigned char *out, int length){
unsigned long crypto_out_len = 16;
unsigned char crypto_out[16] = {0};
.......
//produces 16 bytes output & stores in crypto_out
crypto_function(crypto_out, crypto_out_len);
//lets say crypto_output contents after are : "abcdefghijklmnop"
.......
memcpy(out, crypto_out,length);
return 0;
}
function2(){
unsigned char out[10] = {0};
function1(out, 10);
std::pair<unsigned char *,int> map_entry;
map_entry.first = out;
map_entry.second = 10;
}
現在,map_entry.first應包含以下內容: 「ABCDEFGHIJ」,對不對?
但它包含「abcdefghij#$%f1 ^」,與它關聯的一些垃圾。我應該如何避免這種意外的行爲,以便map_entry.first
應該完全包含「abcdefghij」。
但只是爲了確保memcpy從src複製空字符到dest,對不對? – annunarcist
是的。 'memcpy()'拷貝指定的字節數,根本不關心空字符。 'strcpy()'和'strncpy()'停在空字符處。 –