如何使用calloc重新分配一些内存?
我已使用calloc函数分配了一个字符串:如何使用calloc重新分配一些内存?,c,memory-leaks,realloc,calloc,C,Memory Leaks,Realloc,Calloc,我已使用calloc函数分配了一个字符串: //string1 and string2 previously declared char *stringClone = calloc(strlen(string1) + 1, sizeof(char)); 现在我想用不同的字符串在stringClone上做同样的事情。做: stringClone = calloc(strlen(string2) + 1, sizeof(char)); 我会有一些内存泄漏,对吗?在这种情况下,我应该如何使用rea
//string1 and string2 previously declared
char *stringClone = calloc(strlen(string1) + 1, sizeof(char));
现在我想用不同的字符串在stringClone上做同样的事情。做:
stringClone = calloc(strlen(string2) + 1, sizeof(char));
我会有一些内存泄漏,对吗?在这种情况下,我应该如何使用realloc?您可以使用realloc()
重新分配由malloc()
,calloc()
,realloc()
,对齐的\u alloc()
或strdup()
分配的内存。请注意,如果重新分配的块大于calloc()
返回的原始块,则新分配的部分将初始化为所有零位
但是请注意,realloc()
的语法不是您使用的语法:必须将指针作为第一个参数传递,并为新的大小传递一个size\t
。此外,如果无法分配新块,则会返回NULL
,并且不会释放该块,因此不应将返回值直接存储到stringClone
如果要使用realloc()
,请执行以下操作:
//先前声明的string1和string2
char*stringClone=calloc(strlen(string1)+1,1);
...
char*newp=realloc(stringClone,strlen(string2)+1);
if(newp==NULL){
//处理内存不足的情况
免费(克隆);
}
由于您似乎并不关心stringClone
的内容是否保留在重新分配的块中,因此您可能只需编写:
//先前声明的string1和string2
char*stringClone=calloc(strlen(string1)+1,1);
if(stringClone==NULL){
//处理内存不足的情况
...
}
strcpy(stringClone,string1);
...
免费(克隆);
stringClone=calloc(strlen(string2)+1,1);
if(stringClone==NULL){
//处理内存不足的情况
...
}
strcpy(stringClone,string2);
还要注意,在POSIX兼容系统上,有一个内存分配函数对您的用例非常有用:strdup
获取指向C字符串的指针,分配strlen)+1
字节,将字符串复制到分配的块并返回:
//先前声明的string1和string2
char*stringClone=strdup(string1);
if(stringClone==NULL){
//处理内存不足的情况
...
}
...
免费(克隆);
stringClone=strdup(string2);
if(stringClone==NULL){
//处理内存不足的情况
...
}
还请注意,在C中不需要强制转换malloc
、calloc
和realloc
的返回值,这被认为是错误的样式。您可以使用realloc()
来重新分配malloc()
、calloc()
、realloc()、aligned\u alloc()
或strdup()分配的内存
。请注意,如果重新分配的块大于calloc()
返回的原始块,则新分配的部分将初始化为所有零位
但是请注意,realloc()
的语法不是您使用的语法:必须将指针作为第一个参数传递,并为新的大小传递一个size\t
。此外,如果无法分配新块,则会返回NULL
,并且不会释放该块,因此不应将返回值直接存储到stringClone
如果要使用realloc()
,请执行以下操作:
//先前声明的string1和string2
char*stringClone=calloc(strlen(string1)+1,1);
...
char*newp=realloc(stringClone,strlen(string2)+1);
if(newp==NULL){
//处理内存不足的情况
免费(克隆);
}
由于您似乎并不关心stringClone
的内容是否保留在重新分配的块中,因此您可能只需编写:
//先前声明的string1和string2
char*stringClone=calloc(strlen(string1)+1,1);
if(stringClone==NULL){
//处理内存不足的情况
...
}
strcpy(stringClone,string1);
...
免费(克隆);
stringClone=calloc(strlen(string2)+1,1);
if(stringClone==NULL){
//处理内存不足的情况
...
}
strcpy(stringClone,string2);
还要注意,在POSIX兼容系统上,有一个内存分配函数对您的用例非常有用:strdup
获取指向C字符串的指针,分配strlen)+1
字节,将字符串复制到分配的块并返回:
//先前声明的string1和string2
char*stringClone=strdup(string1);
if(stringClone==NULL){
//处理内存不足的情况
...
}
...
免费(克隆);
stringClone=strdup(string2);
if(stringClone==NULL){
//处理内存不足的情况
...
}
还请注意,在C中不需要强制转换malloc
、calloc
和realloc
的返回值,这被认为是错误的样式。使用realloc
的原因是它保持了原始数据的完整性。但是,如果我正确理解了您的用例,您打算删除原始数据。在这种情况下,只需写下:
char *stringClone = calloc(strlen(string1) + 1, sizeof(char));
// check for calloc error
// use stringClone for string1
free(stringClone);
stringClone = calloc(strlen(string2) + 1, sizeof(char));
// check for calloc error
// use stringClone for string2
calloc
的错误检查比realloc
更简单,因为不需要临时变量。此外,此模式清楚地表明string1
和string2
的数组内容不相关。使用realloc
的原因是它保持了原始数据的完整性。但是,如果我正确理解了您的用例,您打算删除原始数据。在这种情况下,只需写下:
char *stringClone = calloc(strlen(string1) + 1, sizeof(char));
// check for calloc error
// use stringClone for string1
free(stringClone);
stringClone = calloc(strlen(string2) + 1, sizeof(char));
// check for calloc error
// use stringClone for string2
calloc
的错误检查比realloc
更简单,因为不需要临时变量。此外,此模式清楚地表明string1