将 char* 作为参数传递给函数时,被调用函数是否应该对该字符串执行 free 操作?否则数据会“丢失”,程序会泄漏数据。还是编译器以特殊方式处理 char* 以避免每个人都必须一直执行 free 并在超出范围时自动删除它?我将“字符串”传递给函数,而不是将实例传递给已经存在的 char*。还是应该使用 char[] 代替?为参数输入设置固定限制感觉很愚蠢。
7 回答
牢记这个简单的原则:“始终在分配的同一级别释放内存”。换句话说,一个函数不应该尝试释放它自己没有分配的内存。一个简短的例子来澄清这一点:
#include "graphics.h"
// The graphics API will get a Canvas object for us. This may be newly allocated
// or one from a pool of pre-allocated objects.
Canvas* canvas = graphics_get_canvas ();
// If draw_image () frees canvas, that violates the above principle.
// The behavior of the program will be unspecified. So, just draw the image
// and return.
draw_image (canvas);
// This is also a violation.
// free (canvas) ;
// The right thing to do is to give back the Canvas object to the graphics API
// so that it is freed at the same 'level' where it was allocated.
graphics_return_canvas (canvas);
请注意,该函数未命名graphics_free_canvas ()
或类似名称,因为 API 可能会选择释放它或通过将其返回到池中来重用它。关键是,除非我们被明确告知,否则假设拥有我们没有创建的资源的所有权是一种非常糟糕的编程实践。
听起来你在问这种用法:
void foo(char* str);
foo("test string");
这是一种特殊情况;"test string"
是存储在可执行文件中的字符串表中的常量字符串,不需要释放。foo
实际上应该用 aconst char*
来说明这一点,并且char*
在 C++ 中不赞成将字符串文字存储在非常量 s 中
函数是否应该执行 afree
取决于谁拥有该字符串。此代码完全有效,不会导致任何内存泄漏:
int main()
{
char* s = malloc(.....);
f(s);
free(s);
}
如果它拥有字符串的所有权,也free
可以在函数内部执行。f
但是请注意,这是危险的,因为您假设传递给函数f
的字符串总是使用malloc
或相关函数在堆上分配。如果用户将指针传递给堆栈上分配的字符串,您的程序将表现出不可预知的行为。
一般来说,编译器不会对字符串的内存管理进行任何特殊处理。从编译器的角度来看,它只是一堆字符。
看来你已经习惯了 OOP 风格。我不喜欢 OOP,对我来说,如果我在分配后获得一个对象的副本会很奇怪。在这种情况下,字符串位于内存中的某个位置,其地址作为 char* 发送,而不是整个字符串。另外,请注意您只能释放()由 malloc()返回的指针,并且只能释放一次。
有时,API 需要一个分配的缓冲区,并将其交给调用该 api 以释放它的函数的所有者。
myFunc()
{
char *error = malloc(<max size of error string>);
foo(error);
//Free the pointer here
free(error);
}
一些 API 像 GLIB api 的期望指针指向已声明变量的地址
myFunc()
{
GError *error;
glib_api(&error);
if (error)
{
printf("Error %s", error-> message);
// can use glib API to free if error is NON NULL but message is allocated by GLIB API
g_error_free(error);
}
}
因此,即使您没有为变量分配内存,您也需要在使用标准库时进行释放。
一块分配的内存,如果不释放,将导致多进程环境中的内存减少,从而降低系统的性能。
使用 plain char *
,我建议始终使用调用者“拥有”字符串并负责释放它的策略编写代码,如果它是由malloc
. 另一方面,人们当然可以在 C 中设想“伪按值传递”字符串对象,实现为结构,其中策略规定您必须在传递字符串时放弃字符串的所有权(或先复制它并传递副本)作为论据。如果实现对传递的对象只是对存储的引用的字符串使用引用计数存储,那么这可能会特别有效,因此“重复”操作将仅仅是引用计数增量加上微不足道的包装结构分配(或甚至传值结构)。
将 char 作为函数变量的指针是指向同一变量的地址,除非它被替换为常量字符串。您的问题无法用简单的是/否指南来解释;这取决于上下文。在下面的代码中,分别在堆和堆栈上分配的结构通过引用以及字符串 char * 传递,并将数据插入到结构中。请注意 malloc 在何时使用方面有何不同,但函数的工作方式完全相同。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// struct with all data within
typedef struct d
{
int number;
char name[50];
}data;
// struct with dynamic char *
typedef struct d2
{
int number;
char *name;
}dynamic_data;
// generic function placing data into struct
void InsertData ( data * out, int a, char * b )
{
out->number = a;
strcpy(out->name, b);
}
// generic function placing data into second struct
void InsertData2 ( dynamic_data * out, int a, char * b )
{
out->number = a;
strcpy(out->name, b);
}
int main ( void )
{
char * text = "some string\0";
int n = 20;
// allocated struct
data stuff;
dynamic_data stuff2;
dynamic_data * stuff3;
// need to allocate pointer within struct only
stuff2.name = (char *) malloc(50 * sizeof(char));
// heap allocated struct
stuff3 = (dynamic_data * ) malloc(50 * sizeof(dynamic_data));
// heap allocated sub element char *
stuff3->name = (char *) malloc(50 * sizeof(char));
// this is the data
printf ( "Pre insertion data\n" );
printf ( "s=[%s]\n", text );
printf ( "n=%d\n", n );
// this is the function insertting
InsertData ( &stuff, n, text );
printf ( "Post insertion data\n" );
printf ( "stuff.name=[%s]\n", stuff.name );
printf ( "stuff.number=%d\n", stuff.number );
// this is the function inserting
InsertData2 ( &stuff2, n, text );
printf ( "Post insertion data\n" );
printf ( "stuff.name=[%s]\n", stuff2.name );
printf ( "stuff.number=%d\n", stuff2.number );
//
// This is the segfault version - if nothing was allocated for pointers into
// this function scope, it would crash
// this is the function insertting under a heap allocated
InsertData2 ( stuff3, n, text );
printf ( "Post insertion data - dynamic version\n" );
printf ( "stuff3->name=[%s]\n", stuff3->name );
printf ( "stuff3->number=%d\n", stuff3->number );
// free in reverse order
free(stuff3->name);
free(stuff3);
free(stuff2.name);
return 0;
}