简体   繁体   English

如何将已动态分配的字符串复制到已动态分配的另一个字符串?

[英]How do i copy a string that has been dynamically allocated to another string that has been dynamically allocated?

I am having trouble trying to implement a custom strcpy function which is supposed to handle cases where the src string is larger than the destination string. 我在尝试实现自定义strcpy函数时遇到麻烦,该函数应该处理src字符串大于目标字符串的情况。 Here I have provided some code so that you guys can see the entire function. 在这里,我提供了一些代码,以便您可以看到整个功能。 My issue is that every time I increment *dest, it goes into a null address despite the fact that I have allocated enough memory to fit all of src in it. 我的问题是,尽管我分配了足够的内存来容纳所有src,但每次增加* dest时,它都会进入一个空地址。 This causes the a segmentation fault in (double pointer)dest = *src. 这会导致(双指针)dest = * src中的分段错误。 dest is stored as a char** because in reality, the argument that has to be passed is another string that is possibly of a smaller size than src, and I wish to overwrite *dest as safely as I can. dest存储为char **,因为实际上,必须传递的参数是另一个字符串,该字符串的大小可能比src小,我希望尽可能安全地覆盖* dest。

int customStrCpy(char** dest, char* src){
    int strlen1 = strlen(*dest), strlen2 = strlen(src);
    if(strlen1 < strlen2){
        //Creates a dynamically allocated array that is big enough to    store the contents of line2.
        *dest = calloc(strlen2, sizeof(char));
        char* backup_str = *dest;

        int copy_arrs;
        for(copy_arrs = 0; copy_arrs < strlen2; copy_arrs++){
            **dest = *src;
            *dest++; src++;
        }
        *dest = backup_str;
    }
    else strcpy(*dest, src);
}

In the end, (char**)dest is supposed to be pointing to the correct string. 最后,(char **)dest应该指向正确的字符串。

You need to add 1 to the string length, to allow for the null terminator, and you should free the old contents of dest if you're allocating a new string. 您需要在字符串长度上添加1 ,以允许使用空终止符,并且如果要分配新的字符串,则应释放dest的旧内容。 After you do this, you can do the same strcpy() as you do when you don't need to reallocate. 完成此操作后,可以执行与不需要重新分配时相同的strcpy()

There's also no need for the int return type (unless you want to add error checking to malloc() , and return a status result). 也不需要int返回类型(除非您要向malloc()添加错误检查并返回状态结果)。 This function modifies an argument, it should be void . 此函数修改参数,应为void

void customStrCpy(char** dest, char* src){
    int strlen1 = strlen(*dest), strlen2 = strlen(src);
    if(strlen1 < strlen2){
        free(*dest); // Free the old string
        //Creates a dynamically allocated array that is big enough to store the contents of line2.
        *dest = malloc(strlen2+1);
    }
    strcpy(*dest, src); // or memcpy(*dest, src, strlen2+1);
}

Usually strcpy returns char * for "direct" use in other operations. 通常, strcpy返回char *以便在其他操作中“直接”使用。

char *mysStrCpy(char **dest, const char *src)
{
    size_t len = strlen(src);
    char *tmpptr;

    *dest = malloc(len + 1);
    // or *dest = realloc(*dest, len + 1);
    if(*dest)
    {
        tmpptr = *dest;
        while(*tmpptr++ = *src++);
    }
    return *dest;
}
 *dest++;

increments dest, not the pointer dest points to. 递增dest,而不是指针dest指向的指针。 You want: 你要:

(*dest)++;

ps: there are better ways to accomplish what you are after.... ps:有更好的方法来完成您追求的目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何释放一个函数从另一个函数动态分配的内存? - How can I free memory that has been dynamically allocated by one function from another function? 如何手动填充已通过整数指针动态分配的整数数组? - How to manually fill an integer array that has been dynamically allocated via an integer pointer? 如何最小化 C 中动态分配的字符串数组? - How do I minimize a dynamically allocated string array in C? 释放已分配给char指针(字符串)数组的内存。 我需要释放每个字符串还是仅释放“主”指针? - Freeing memory which has been allocated to an array of char pointers (strings). Do I have to free each string or just the “main” pointer? Malloc - &gt;分配了多少内存? - Malloc -> how much memory has been allocated? 如何将字符串的副本复制到适当大小的动态分配字符数组中? - how to make a copy of string into a dynamically allocated character array of the appropriate size? 动态分配的 function 中的局部指针变量会发生什么情况? - What happens to a local pointer variable inside a function that has been dynamically allocated? 如何在字符串数组中为某个新字符串动态分配内存并将该字符串复制到 C 中动态分配的空间中? - How to dynamically allocate memory in an array of strings for some new string and copy that string into the dynamically allocated space in C? 如何释放划分为矩阵的动态分配的字符串? - How to free a dynamically allocated string divided into a matrix? 如何使用管道发送动态分配的字符串 - how to send dynamically allocated string using pipe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM