[英]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.