C-Memncpy/Strncpy(也尝试了strncat)复制的字符比应该的少1个,非常感谢您的帮助

C-Memncpy/Strncpy(也尝试了strncat)复制的字符比应该的少1个,非常感谢您的帮助,c,pointers,binary,size,memcpy,C,Pointers,Binary,Size,Memcpy,就像我说的,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 &l

就像我说的,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;
}
#定义位金额4
char*随机二进制生成器(char*随机){
int randomNum,i;
焦炭温度;
char*temptr=(char*)malloc(1*sizeof(char));
对于(i=0;i一些问题

main
中,数组/指针的大小需要允许nul终止符,因此它们需要
BIT\u AMOUNT+1

main
中,您的
memcpy
不会复制nul终止符。请改用
strcpy

添加起始nul最容易使用(例如):

不要施放malloc

sizeof(char)
始终是1(根据定义),而不管实际的、与体系结构相关的大小(例如
char
实际上是16位)。因此,不要使用
sizeof(char)

randomBinaryGenerator
中,
temptr
泄漏内存。不需要
malloc
[甚至不需要
temptr
)。改用
char temp[2];

sizeof(bit\u bitKey
)始终是常量,因为它是指针的大小,而不是指针指向的内容(即它不是
bit\u 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;
}

请注意,
temptr=NULL;temptr=&temp;
正在覆盖由
malloc
返回的指针(指向1字节!)。旁白:而不是调用
strcat中的
i(随机,temptr);
这两个参数都需要指向以null结尾的字符串。但是
temptr
指向一个
char
变量,该变量不包含
'\0'
,因此它不是以null结尾的字符串。生成纯文本图像并不能使任何内容更易于理解。它只会阻止选择和复制任何内容。此外在调用中,
strcat(random,temptr);
random
指向调用方(
main()
)分配的
malloc()
内存块,但不是以null结尾的。哇,非常感谢:)老实说,我发现内存分配有点混乱,这导致我编写了混乱的代码。我真的很感谢你的帮助。如果你不介意的话,你能为我指出正确的方向,让我了解我应该阅读的概念以及我应该检查和研究的资源吗?到目前为止,我喜欢C,但它肯定对我来说有点太复杂了有时。上帝保佑:)这个答案有内存泄漏,因为通过调用
free(bin\u bitKey);
永远不会返回char*bin\u bitKey=malloc(BIT\u AMOUNT+1);
分配的内存。
#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;
}
#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;
}