如何重新分配一些使用 calloc 分配的内存?
How to realloc some memory allocated using calloc?
我用 calloc 函数分配了一个字符串:
//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()
、aligned_alloc()
或 strdup()
分配的内存。请注意,如果重新分配的块大于由 calloc()
编辑的原始块 return,则新分配的部分将 not 初始化为所有位零。
但是请注意,realloc()
的语法不是您使用的语法:您必须将指针作为第一个参数传递,并为新大小传递一个 size_t
。此外,如果无法分配新块,NULL
将被 returned 并且该块不会被释放,因此您不应将 return 值直接存储到 stringClone
。
如果你想使用 realloc()
,你应该这样做:
//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
的内容是否保留在重新分配的块中,您应该简单地写:
//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
字节,将字符串复制到分配的块并 returns 它:
//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
的 return 值在 C 中是不必要的,并且被认为是糟糕的风格。
使用realloc
的原因是它保持了原始数据的完整性。但是,如果我理解你的 use-case 正确,你打算删除原始数据。在那种情况下,只写:
更简单明了
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
error-checking 对于 calloc
比 realloc
更简单,因为不需要临时变量。此外,此模式清楚地表明 string1
和 string2
的数组内容不相关。
我用 calloc 函数分配了一个字符串:
//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()
、aligned_alloc()
或 strdup()
分配的内存。请注意,如果重新分配的块大于由 calloc()
编辑的原始块 return,则新分配的部分将 not 初始化为所有位零。
但是请注意,realloc()
的语法不是您使用的语法:您必须将指针作为第一个参数传递,并为新大小传递一个 size_t
。此外,如果无法分配新块,NULL
将被 returned 并且该块不会被释放,因此您不应将 return 值直接存储到 stringClone
。
如果你想使用 realloc()
,你应该这样做:
//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
的内容是否保留在重新分配的块中,您应该简单地写:
//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
字节,将字符串复制到分配的块并 returns 它:
//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
的 return 值在 C 中是不必要的,并且被认为是糟糕的风格。
使用realloc
的原因是它保持了原始数据的完整性。但是,如果我理解你的 use-case 正确,你打算删除原始数据。在那种情况下,只写:
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
error-checking 对于 calloc
比 realloc
更简单,因为不需要临时变量。此外,此模式清楚地表明 string1
和 string2
的数组内容不相关。