用新字符串替换堆上的字符串
Replacing strings on the heap with new ones
我觉得使用动态类型语言已经颠覆了我的直觉!
假设我 malloc
space 一个字符串,然后用另一个 malloc
(它使用第一个数据)更新那个指针,这是内存泄漏吗?
char* my_string = (char*)malloc(length + 1);
(void)snprintf(my_string, length, "blah...");
my_string = some_manipulation(my_string);
我们 char* some_manipulation(const char* str);
为其输出分配内存,它是根据提供的参数生成的(并且长度可能不同)。
第一个malloc
现在丢失,但占据space,直到退出?
如果您在同一个指针上有两个 malloc
调用和一个 free
调用,您 几乎可以肯定 有泄漏(缺少第一个 malloc 失败).
每个成功的 malloc
都应该在指针生命周期结束的某个地方有一个关联的 free
。
泄漏:
foo* bar = NULL;
bar = malloc(sizeof(foo) * 10);
if (bar) {
bar = malloc(sizeof(foo) * 20);
}
else {
fprintf(stderr, "Error: Could not allocate memory to bar\n");
exit(EXIT_FAILURE);
}
free(bar);
bar = NULL;
无泄漏:
foo* bar = NULL;
bar = malloc(sizeof(foo) * 10);
if (bar) {
free(bar);
bar = NULL;
}
else {
fprintf(stderr, "Error: Could not allocate memory to bar\n");
exit(EXIT_FAILURE);
}
bar = malloc(sizeof(foo) * 20);
if (bar) {
free(bar);
bar = NULL;
}
else {
fprintf(stderr, "Error: Could not allocate memory to bar\n");
exit(EXIT_FAILURE);
}
Say if I malloc
space for a string and then update that pointer with another malloc
(which uses the data from the first), is that a memory leak?
是的,如果您不存储 string
的第一个值,则在第二次调用 malloc()
.
覆盖它之前
漏码
char * p = malloc(42);
p = malloc(41);
/* here the program leaks 42 bytes. */
你只能free()
第二次调用的41个字节
free(p);
因为对 42 字节块的引用丢失了。
不漏码
char * p = malloc(42);
char * q = p;
p = malloc(41);
这里没有泄漏,你仍然可以这样做:
free(p); /* Frees 41 bytes. */
free(q); /* Frees 42 bytes */
which uses the data from the first
所有这些完全不取决于在分配的内存中存储了(或不存储)的内容。
当然可以。您似乎将这里的字符串视为 不可变对象 这是一个不错的概念,但不会影响您释放占用的内存。
如果您知道对您的函数的调用在概念上会使输入字符串无效(因此,调用者在调用该函数后将不再需要它),您可以改为执行以下操作:
int some_manipulation(char **str)
{
char *ret = malloc(...);
/* handle error, return -1 */
/* do whatever manipulation */
free(*str);
*str = ret;
return 0;
}
我觉得使用动态类型语言已经颠覆了我的直觉!
假设我 malloc
space 一个字符串,然后用另一个 malloc
(它使用第一个数据)更新那个指针,这是内存泄漏吗?
char* my_string = (char*)malloc(length + 1);
(void)snprintf(my_string, length, "blah...");
my_string = some_manipulation(my_string);
我们 char* some_manipulation(const char* str);
为其输出分配内存,它是根据提供的参数生成的(并且长度可能不同)。
第一个malloc
现在丢失,但占据space,直到退出?
如果您在同一个指针上有两个 malloc
调用和一个 free
调用,您 几乎可以肯定 有泄漏(缺少第一个 malloc 失败).
每个成功的 malloc
都应该在指针生命周期结束的某个地方有一个关联的 free
。
泄漏:
foo* bar = NULL;
bar = malloc(sizeof(foo) * 10);
if (bar) {
bar = malloc(sizeof(foo) * 20);
}
else {
fprintf(stderr, "Error: Could not allocate memory to bar\n");
exit(EXIT_FAILURE);
}
free(bar);
bar = NULL;
无泄漏:
foo* bar = NULL;
bar = malloc(sizeof(foo) * 10);
if (bar) {
free(bar);
bar = NULL;
}
else {
fprintf(stderr, "Error: Could not allocate memory to bar\n");
exit(EXIT_FAILURE);
}
bar = malloc(sizeof(foo) * 20);
if (bar) {
free(bar);
bar = NULL;
}
else {
fprintf(stderr, "Error: Could not allocate memory to bar\n");
exit(EXIT_FAILURE);
}
Say if I
malloc
space for a string and then update that pointer with anothermalloc
(which uses the data from the first), is that a memory leak?
是的,如果您不存储 string
的第一个值,则在第二次调用 malloc()
.
漏码
char * p = malloc(42);
p = malloc(41);
/* here the program leaks 42 bytes. */
你只能free()
第二次调用的41个字节
free(p);
因为对 42 字节块的引用丢失了。
不漏码
char * p = malloc(42);
char * q = p;
p = malloc(41);
这里没有泄漏,你仍然可以这样做:
free(p); /* Frees 41 bytes. */
free(q); /* Frees 42 bytes */
which uses the data from the first
所有这些完全不取决于在分配的内存中存储了(或不存储)的内容。
当然可以。您似乎将这里的字符串视为 不可变对象 这是一个不错的概念,但不会影响您释放占用的内存。
如果您知道对您的函数的调用在概念上会使输入字符串无效(因此,调用者在调用该函数后将不再需要它),您可以改为执行以下操作:
int some_manipulation(char **str)
{
char *ret = malloc(...);
/* handle error, return -1 */
/* do whatever manipulation */
free(*str);
*str = ret;
return 0;
}