问题描述
我已经用 calloc 函数分配了一个字符串:
I've allocated a string with the calloc function:
//string1 and string2 previously declared
char *stringClone = calloc(strlen(string1) + 1, sizeof(char));
现在我想用不同的字符串在 stringClone 上做同样的事情.做:
Now I want to do the same thing on stringClone with a different string. Doing:
stringClone = calloc(strlen(string2) + 1, sizeof(char));
我会有一些内存泄漏,对吧?在这种情况下,我应该如何使用 realloc?
I'm gonna have some memory leak, right? How should I use the realloc in this case?
推荐答案
您可以使用 realloc()
重新分配 malloc()
、calloc 分配的内存()
、realloc()
、aligned_alloc()
或 strdup()
.请注意,如果重新分配的块大于 calloc()
返回的原始块,则新分配的部分将不会初始化为所有位为零.
You can use realloc()
to reallocate memory allocated by malloc()
, calloc()
, realloc()
, aligned_alloc()
or strdup()
. Note that if the reallocated block is larger than the original block returned by calloc()
, the newly allocated portion will not be initialized to all bits zero.
但是请注意,realloc()
的语法不是您使用的:您必须将指针作为第一个参数传递,并为新大小传递一个 size_t
.此外,如果无法分配新块,则返回 NULL
并且不会释放该块,因此不应将返回值直接存储到 stringClone
.
Note however that the syntax for realloc()
is not what you use: you must pass the pointer as the first argument and a single size_t
for the new size. Furthermore, if a new block cannot be allocated, NULL
is returned and the block is not freed, hence you should not store the return value directly to stringClone
.
如果你想使用realloc()
,你应该这样做:
If you want to use realloc()
, here is what you should do:
//string1 and string2 previously declared
char *stringClone = calloc(strlen(string1) + 1, 1);
...
char *newp = realloc(stringClone, strlen(string2) + 1);
if (newp == NULL) {
// deal with out of memory condition
free(stringClone);
}
由于您似乎并不关心 stringClone
的内容是否保留在重新分配的块中,您可能应该简单地编写:
Since you do not seem to care that the contents of stringClone
be preserved in the the reallocated block, you should probably simply write:
//string1 and string2 previously declared
char *stringClone = calloc(strlen(string1) + 1, 1);
if (stringClone == NULL) {
// deal with out of memory condition
...
}
strcpy(stringClone, string1);
...
free(stringClone);
stringClone = calloc(strlen(string2) + 1, 1);
if (stringClone == NULL) {
// deal with out of memory condition
...
}
strcpy(stringClone, string2);
还要注意,在符合 POSIX 的系统上,有一个内存分配函数对您的用例非常有用:strdup(s)
获取一个指向 C 字符串的指针,分配 strlen(s) + 1
字节,将字符串复制到分配的块并返回:
Note also that on POSIX compliant systems, there is a memory allocation function that is very useful for your use case: strdup(s)
takes a pointer to a C string, allocates strlen(s) + 1
bytes, copies the string to the allocated block and returns it:
//string1 and string2 previously declared
char *stringClone = strdup(string1);
if (stringClone == NULL) {
// deal with out of memory condition
...
}
...
free(stringClone);
stringClone = strdup(string2);
if (stringClone == NULL) {
// deal with out of memory condition
...
}
还要注意,转换 malloc
、calloc
和 realloc
的返回值在 C 中是不必要的,被认为是不好的风格.
Note also that casting the return value of malloc
, calloc
and realloc
is unnecessary in C and considered bad style.
这篇关于如何重新分配一些使用 calloc 分配的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!