c中的memcpy之后字符串末尾的垃圾字符追加
Junk character appendind at end of string after memcpy in c
我在学C。
在下面的代码中,当我尝试执行 memcpy 时,它在末尾添加了垃圾字符。
没有得到我所缺少的。
请帮忙
int threshold = passlen - type;
printf("THRESHOLD: %d\n",threshold);
printf("TYPE: %d\n",type);
printf("decodedpass: %s len(decodepass): %d\n",decodedpass,strlen(decodedpass));
strncpy(salt, &decodedpass[threshold] , type);
printf("-------SALT: %s\n",salt);
salt[type] = '[=10=]';
printf("-------LASTSALT: %s\n",salt);
saltlen = strlen(salt);
printf("-------SALTLEN: %d\n",saltlen);
int len = saltlen + userpasslen;
printf("-------USERPASS: %s\n",userpass);
printf("-------LENSALTandUSERPASSLEN: %d\n",len);
createpass = xcalloc(len, sizeof(char));
memcpy(createpass, userpass, userpasslen);
printf("-------len(createpass):%d userpasslen:%d len(salt):%d saltlen:%d salt:%s\n",strlen(createpass),userpasslen,strlen(salt),saltlen,salt);
printf("-------CREATEPASSmemcpy1: %s\n",createpass);
memcpy(createpass + userpasslen, salt, saltlen);
printf("-------CREATEPASSmemcpy2: %s len(createpass):%d\n",createpass,strlen(createpass));
输出:
THRESHOLD: 20
TYPE: 8
decodedpass: G�f�ɴ�=-$�o� :K���R12345678 len(decodepass): 28
-------SALT: 12345678
-------LASTSALT: 12345678
-------SALTLEN: 8
-------USERPASS: 1234567890123456
-------LENSALTandUSERPSSLEN: 24
-------len(createpass):16 userpasslen:16 len(salt):8 saltlen:8 salt:12345678
-------CREATEPASSmemcpy1: 1234567890123456
-------CREATEPASSmemcpy2: 123456789012345612345678� len(createpass):26
因为你在内存中的字符串末尾没有 null 章程。在分配内存时,必须考虑这种情况。那么,有两种方法可以解决您的问题:
1)
createpass = xcalloc(len+1, sizeof(char));
memset(createpass, 0, len+1);
或
2)
createpass = xcalloc(len+1, sizeof(char));
memset(createpass, 0, len+1); // option, because the 'sprintf' will fill a '[=11=]' at the end of string automatically.
sprintf(createpass, "%s%s", userpass, salt);
尽情享受吧。
我在学C。 在下面的代码中,当我尝试执行 memcpy 时,它在末尾添加了垃圾字符。 没有得到我所缺少的。 请帮忙
int threshold = passlen - type;
printf("THRESHOLD: %d\n",threshold);
printf("TYPE: %d\n",type);
printf("decodedpass: %s len(decodepass): %d\n",decodedpass,strlen(decodedpass));
strncpy(salt, &decodedpass[threshold] , type);
printf("-------SALT: %s\n",salt);
salt[type] = '[=10=]';
printf("-------LASTSALT: %s\n",salt);
saltlen = strlen(salt);
printf("-------SALTLEN: %d\n",saltlen);
int len = saltlen + userpasslen;
printf("-------USERPASS: %s\n",userpass);
printf("-------LENSALTandUSERPASSLEN: %d\n",len);
createpass = xcalloc(len, sizeof(char));
memcpy(createpass, userpass, userpasslen);
printf("-------len(createpass):%d userpasslen:%d len(salt):%d saltlen:%d salt:%s\n",strlen(createpass),userpasslen,strlen(salt),saltlen,salt);
printf("-------CREATEPASSmemcpy1: %s\n",createpass);
memcpy(createpass + userpasslen, salt, saltlen);
printf("-------CREATEPASSmemcpy2: %s len(createpass):%d\n",createpass,strlen(createpass));
输出:
THRESHOLD: 20
TYPE: 8
decodedpass: G�f�ɴ�=-$�o� :K���R12345678 len(decodepass): 28
-------SALT: 12345678
-------LASTSALT: 12345678
-------SALTLEN: 8
-------USERPASS: 1234567890123456
-------LENSALTandUSERPSSLEN: 24
-------len(createpass):16 userpasslen:16 len(salt):8 saltlen:8 salt:12345678
-------CREATEPASSmemcpy1: 1234567890123456
-------CREATEPASSmemcpy2: 123456789012345612345678� len(createpass):26
因为你在内存中的字符串末尾没有 null 章程。在分配内存时,必须考虑这种情况。那么,有两种方法可以解决您的问题:
1)
createpass = xcalloc(len+1, sizeof(char));
memset(createpass, 0, len+1);
或
2)
createpass = xcalloc(len+1, sizeof(char));
memset(createpass, 0, len+1); // option, because the 'sprintf' will fill a '[=11=]' at the end of string automatically.
sprintf(createpass, "%s%s", userpass, salt);
尽情享受吧。