就像我说的,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
数组指针的大小需要考虑到nul终结符,所以它们需要是 BIT_AMOUNT + 1
在 main
,你的 memcpy
是否 不 复制nul终止符。使用 strcpy
来代替。
添加起始nul最容易的方法是(例如)。
*random = 0;
不要投 malloc
: 我是否要投掷malloc的结果?
sizeof(char)
是 始终 1 (根据定义),不管实际的,依赖于架构的大小(例如 char
实际上是16位)。) 所以,不要使用 sizeof(char)
在 randomBinaryGenerator
, tempPtr
泄漏内存。不需要 malloc
[甚至 tempPtr
根本]。使用 char temp[2];
而不是。
sizeof(bit_bitKey
)总是恒定的,因为它的大小是由 指针 和 不 它所指向的事物(即它是 不 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
更快更好 橆 使用 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;
}