C - Memncpy/Strncpy(也试过 strncat)复制了比应有的少 1 个字符,感谢帮助
C - Memncpy/Strncpy (tried strncat too) copies 1 character less than it should, help is appreciated
就像我说的,memncpy()(在 main 的中间)复制了比应有的少 1 个字符,不知道为什么。我添加了评论和图像以使其更易于理解。
#define BIT_AMOUNT 4
char * randomBinaryGenerator(char * random){
int randomNum, i;
char temp;
char * tempPtr = (char *)malloc(1 * sizeof(char));
for(i = 0; i <= BIT_AMOUNT - 1; i++){
randomNum = rand() % 2;
temp = randomNum + '0';
tempPtr = NULL;
tempPtr = &temp;
strcat(random, tempPtr);
}
return random;
}
int main(){
srand(time(0));
char str_bin_bitKey[BIT_AMOUNT] = "";
char * random = (char *)malloc(BIT_AMOUNT * sizeof(char));
char * bin_bitKey = (char *)malloc(BIT_AMOUNT * sizeof(char));
printf("\nSize of str_bin_bitKey: %ld, Size of bin_bitKey: %ld\n", sizeof(str_bin_bitKey), sizeof(bin_bitKey));
bin_bitKey = randomBinaryGenerator(random); //generates 4 bit long binary number
memcpy(str_bin_bitKey, bin_bitKey, BIT_AMOUNT);//copies 1 character less
printf("\nbin_bitKey: %s\n", bin_bitKey); //4 bits
printf("\nstr_bin_bitKey: %s\n", str_bin_bitKey);//3 bits???
long long dec_bitKey = 0;//unimportant for now .... convertBinaryToDecimal(bin_bitKey);
printf("\ndec_bitKey: %lld\n\n", dec_bitKey);
free(random);
return 0;
}
这是输出的样子,如您所见,str_bin_bitKey 是 3 个字符而不是 4 个:
感谢所有帮助。
几个问题...
在main
中,arrays/pointers的大小需要考虑到nul终止符,所以它们需要是BIT_AMOUNT + 1
在 main
中,您的 memcpy
不会 复制 nul 终止符。请改用 strcpy
。
添加起始 nul 最容易完成(例如):
*random = 0;
不投malloc
:Do I cast the result of malloc?
sizeof(char)
总是 1(根据定义),无论实际的架构相关大小如何(例如 char
实际上是 16 位)。所以,不要使用 sizeof(char)
在 randomBinaryGenerator
中,tempPtr
内存泄漏。根本不需要 malloc
[甚至 tempPtr
]。请改用 char temp[2];
。
sizeof(bit_bitKey
) 总是不变的,因为它是 pointer 的大小,而 not 它指向什么(即不是BIT_AMOUNT
).
randomBinaryGenerator
几乎需要完全返工。
这是您的代码的注释和修复版本。我添加了:
#if 0
// old/original code
#else
// new/fixed code
#endif
帮助显示更改。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BIT_AMOUNT 4
char *
randomBinaryGenerator(char *random)
{
int randomNum, i;
#if 0
char temp;
char *tempPtr = malloc(1);
#else
char temp[2];
#endif
#if 1
// add nul terminator
*random = 0;
temp[1] = 0;
#endif
#if 0
for (i = 0; i <= BIT_AMOUNT - 1; i++) {
#else
for (i = 0; i < BIT_AMOUNT; i++) {
#endif
randomNum = rand() % 2;
#if 0
temp = randomNum + '0';
tempPtr = NULL;
tempPtr = &temp;
strcat(random, tempPtr);
#else
temp[0] = randomNum + '0';
strcat(random, temp);
#endif
}
return random;
}
int
main(void)
{
srand(time(0));
// NOTE/BUG: need space for EOS terminator
#if 0
char str_bin_bitKey[BIT_AMOUNT] = "";
char *random = malloc(BIT_AMOUNT);
char *bin_bitKey = malloc(BIT_AMOUNT);
#else
char str_bin_bitKey[BIT_AMOUNT + 1] = "";
char *random = malloc(BIT_AMOUNT + 1);
char *bin_bitKey = malloc(BIT_AMOUNT + 1);
#endif
// NOTE/BUG: sizeof(bit_bitKey) is the size of the _pointer_ and _not_ what
// it points to (i.e. it is _not_ BIT_AMOUNT)
#if 0
printf("\nSize of str_bin_bitKey: %ld, Size of bin_bitKey: %ld\n",
sizeof(str_bin_bitKey), sizeof(bin_bitKey));
#endif
// generates 4 bit long binary number
bin_bitKey = randomBinaryGenerator(random);
// copies 1 character less
#if 0
memcpy(str_bin_bitKey, bin_bitKey, BIT_AMOUNT);
#else
strcpy(str_bin_bitKey, bin_bitKey);
#endif
// 4 bits
printf("\nbin_bitKey: %s\n", bin_bitKey);
// 3 bits???
printf("\nstr_bin_bitKey: %s\n", str_bin_bitKey);
// unimportant for now .... convertBinaryToDecimal(bin_bitKey);
long long dec_bitKey = 0;
printf("\ndec_bitKey: %lld\n\n", dec_bitKey);
free(random);
return 0;
}
这是经过清理和改进的版本。请注意 randomBinaryGenerator
是 faster/better 而 根本不使用 strcat
。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BIT_AMOUNT 4
char *
randomBinaryGenerator(char *random)
{
int randomNum, i;
for (i = 0; i < BIT_AMOUNT; i++) {
randomNum = rand() % 2;
random[i] = randomNum + '0';
}
// add nul terminator
random[i] = 0;
return random;
}
int
main(void)
{
srand(time(0));
char str_bin_bitKey[BIT_AMOUNT + 1];
char *random = malloc(BIT_AMOUNT + 1);
char *bin_bitKey = malloc(BIT_AMOUNT + 1);
// generates 4 bit long binary number
bin_bitKey = randomBinaryGenerator(random);
strcpy(str_bin_bitKey, bin_bitKey);
// 4 bits
printf("\nbin_bitKey: '%s'\n", bin_bitKey);
// 3 bits???
printf("\nstr_bin_bitKey: '%s'\n", str_bin_bitKey);
// unimportant for now .... convertBinaryToDecimal(bin_bitKey);
long long dec_bitKey = 0;
printf("\ndec_bitKey: %lld\n\n", dec_bitKey);
free(random);
return 0;
}
就像我说的,memncpy()(在 main 的中间)复制了比应有的少 1 个字符,不知道为什么。我添加了评论和图像以使其更易于理解。
#define BIT_AMOUNT 4
char * randomBinaryGenerator(char * random){
int randomNum, i;
char temp;
char * tempPtr = (char *)malloc(1 * sizeof(char));
for(i = 0; i <= BIT_AMOUNT - 1; i++){
randomNum = rand() % 2;
temp = randomNum + '0';
tempPtr = NULL;
tempPtr = &temp;
strcat(random, tempPtr);
}
return random;
}
int main(){
srand(time(0));
char str_bin_bitKey[BIT_AMOUNT] = "";
char * random = (char *)malloc(BIT_AMOUNT * sizeof(char));
char * bin_bitKey = (char *)malloc(BIT_AMOUNT * sizeof(char));
printf("\nSize of str_bin_bitKey: %ld, Size of bin_bitKey: %ld\n", sizeof(str_bin_bitKey), sizeof(bin_bitKey));
bin_bitKey = randomBinaryGenerator(random); //generates 4 bit long binary number
memcpy(str_bin_bitKey, bin_bitKey, BIT_AMOUNT);//copies 1 character less
printf("\nbin_bitKey: %s\n", bin_bitKey); //4 bits
printf("\nstr_bin_bitKey: %s\n", str_bin_bitKey);//3 bits???
long long dec_bitKey = 0;//unimportant for now .... convertBinaryToDecimal(bin_bitKey);
printf("\ndec_bitKey: %lld\n\n", dec_bitKey);
free(random);
return 0;
}
这是输出的样子,如您所见,str_bin_bitKey 是 3 个字符而不是 4 个:
感谢所有帮助。
几个问题...
在main
中,arrays/pointers的大小需要考虑到nul终止符,所以它们需要是BIT_AMOUNT + 1
在 main
中,您的 memcpy
不会 复制 nul 终止符。请改用 strcpy
。
添加起始 nul 最容易完成(例如):
*random = 0;
不投malloc
:Do I cast the result of malloc?
sizeof(char)
总是 1(根据定义),无论实际的架构相关大小如何(例如 char
实际上是 16 位)。所以,不要使用 sizeof(char)
在 randomBinaryGenerator
中,tempPtr
内存泄漏。根本不需要 malloc
[甚至 tempPtr
]。请改用 char temp[2];
。
sizeof(bit_bitKey
) 总是不变的,因为它是 pointer 的大小,而 not 它指向什么(即不是BIT_AMOUNT
).
randomBinaryGenerator
几乎需要完全返工。
这是您的代码的注释和修复版本。我添加了:
#if 0
// old/original code
#else
// new/fixed code
#endif
帮助显示更改。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BIT_AMOUNT 4
char *
randomBinaryGenerator(char *random)
{
int randomNum, i;
#if 0
char temp;
char *tempPtr = malloc(1);
#else
char temp[2];
#endif
#if 1
// add nul terminator
*random = 0;
temp[1] = 0;
#endif
#if 0
for (i = 0; i <= BIT_AMOUNT - 1; i++) {
#else
for (i = 0; i < BIT_AMOUNT; i++) {
#endif
randomNum = rand() % 2;
#if 0
temp = randomNum + '0';
tempPtr = NULL;
tempPtr = &temp;
strcat(random, tempPtr);
#else
temp[0] = randomNum + '0';
strcat(random, temp);
#endif
}
return random;
}
int
main(void)
{
srand(time(0));
// NOTE/BUG: need space for EOS terminator
#if 0
char str_bin_bitKey[BIT_AMOUNT] = "";
char *random = malloc(BIT_AMOUNT);
char *bin_bitKey = malloc(BIT_AMOUNT);
#else
char str_bin_bitKey[BIT_AMOUNT + 1] = "";
char *random = malloc(BIT_AMOUNT + 1);
char *bin_bitKey = malloc(BIT_AMOUNT + 1);
#endif
// NOTE/BUG: sizeof(bit_bitKey) is the size of the _pointer_ and _not_ what
// it points to (i.e. it is _not_ BIT_AMOUNT)
#if 0
printf("\nSize of str_bin_bitKey: %ld, Size of bin_bitKey: %ld\n",
sizeof(str_bin_bitKey), sizeof(bin_bitKey));
#endif
// generates 4 bit long binary number
bin_bitKey = randomBinaryGenerator(random);
// copies 1 character less
#if 0
memcpy(str_bin_bitKey, bin_bitKey, BIT_AMOUNT);
#else
strcpy(str_bin_bitKey, bin_bitKey);
#endif
// 4 bits
printf("\nbin_bitKey: %s\n", bin_bitKey);
// 3 bits???
printf("\nstr_bin_bitKey: %s\n", str_bin_bitKey);
// unimportant for now .... convertBinaryToDecimal(bin_bitKey);
long long dec_bitKey = 0;
printf("\ndec_bitKey: %lld\n\n", dec_bitKey);
free(random);
return 0;
}
这是经过清理和改进的版本。请注意 randomBinaryGenerator
是 faster/better 而 根本不使用 strcat
。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BIT_AMOUNT 4
char *
randomBinaryGenerator(char *random)
{
int randomNum, i;
for (i = 0; i < BIT_AMOUNT; i++) {
randomNum = rand() % 2;
random[i] = randomNum + '0';
}
// add nul terminator
random[i] = 0;
return random;
}
int
main(void)
{
srand(time(0));
char str_bin_bitKey[BIT_AMOUNT + 1];
char *random = malloc(BIT_AMOUNT + 1);
char *bin_bitKey = malloc(BIT_AMOUNT + 1);
// generates 4 bit long binary number
bin_bitKey = randomBinaryGenerator(random);
strcpy(str_bin_bitKey, bin_bitKey);
// 4 bits
printf("\nbin_bitKey: '%s'\n", bin_bitKey);
// 3 bits???
printf("\nstr_bin_bitKey: '%s'\n", str_bin_bitKey);
// unimportant for now .... convertBinaryToDecimal(bin_bitKey);
long long dec_bitKey = 0;
printf("\ndec_bitKey: %lld\n\n", dec_bitKey);
free(random);
return 0;
}