Say I have the function below :
说我有以下功能:
char* fakeTrim(char* input) {
char* temp = malloc(strlen(input));
int count = 0;
for(int i = 0; i < strlen(input); i++) {
if(input[i] != ' ')
temp[count++] = input[i];
}
temp[count] = '
Say I have the function below :
说我有以下功能:
char* fakeTrim(char* input) {
char* temp = malloc(strlen(input));
int count = 0;
for(int i = 0; i < strlen(input); i++) {
if(input[i] != ' ')
temp[count++] = input[i];
}
temp[count] = '\0';
return temp;
}
Does the temp cause any memory leakage? IF so, is it possible to free it before we return temp?
温度会导致任何内存泄漏吗?如果是这样,是否有可能在我们返回临时之前释放它?
Thanks!
3 个解决方案
#1
0
No, of course you can't free memory that belongs to the data you're returning. And indeed allocating memory within a utility function like this makes memory leaks extremely likely; since the caller won't see the function body, it will be very easy for her to forget to free it. There is a standard solution to this issue actually, and that is to make the caller allocate the memory himself:
不,当然你不能释放属于你要返回的数据的内存。事实上,在像这样的效用函数中分配内存极有可能导致内存泄漏;由于呼叫者不会看到功能体,因此她很容易忘记释放它。实际上有这个问题的标准解决方案,那就是让调用者自己分配内存:
void fakeTrim(const char* input, char* temp) {
int count = 0;
for(int i = 0; i < strlen(input); i++) {
if(input[i] != ' ')
temp[count++] = input[i];
}
temp[count] = '\0';
}
Now memory leaks are still possible but it's not 'your fault'--the caller should know to free memory that he allocates. Note the addition of const in the signature makes which argument is input, and which is output, clear.
现在内存泄漏仍然存在,但这不是“你的错” - 调用者应该知道释放他分配的内存。请注意,在签名中添加const会使输入哪个参数,以及输出清除。
Edit: Here's a use case:
编辑:这是一个用例:
const char* input = "Hello world";
char* temp = malloc(strlen(input)+1);
fakeTrim(input, temp);
// ... do something with temp
free(temp);
#2
3
No, you can only free the allocated memory when you no longer need to refer to it, which means that the caller needs to free the returned value (and you should mention this in the documentation for the function).
不,你只能在不再需要引用它时释放分配的内存,这意味着调用者需要释放返回的值(你应该在函数的文档中提到它)。
By the way, you will end up with an out-of-bounds array reference on temp[count] = '\0'; if your input string has no spaces in it, so you should allocate one more byte. (And trim doesn't usually remove internal spaces, but perhaps that is why you called it fakeTrim.)
顺便说一下,你最终会得到一个关于temp [count] ='\ 0'的越界数组引用;如果您的输入字符串中没有空格,那么您应该再分配一个字节。 (并且修剪通常不会删除内部空间,但也许这就是你称之为fakeTrim的原因。)
#3
0
Yes, Indeed temp will cause memory leakage.
是的,确实温度会导致内存泄漏。
However, I have an alternative solution for a requirement of yours. You can make use of a dynamic variable in the caller function by using pass by reference and later free it from the caller function itself (No need to free it from the called function). Please see below:
但是,我有一个替代解决方案,以满足您的要求。您可以通过使用传递引用来使用调用函数中的动态变量,然后将其从调用函数本身释放(无需将其从被调用函数中释放)。请看下面:
void main()
{
char *test;
setVal(test);
puts(test);
if(test){
free(test);
test = NULL;
}
}
void setVal(char **data)
{
char retry[100]="This is test";
char *ret;
ret = malloc((strlen(retry))*sizeof(char));
if(ret == NULL)
exit(1);
strncpy(ret, retry, strlen(retry));
*data = ret;
}
If you are still not satisfied. Use the sample codes I have provided with VALGRIND to see if there is any memory leakage.
如果你仍然不满意。使用我在VALGRIND中提供的示例代码来查看是否存在任何内存泄漏。
Hope it helps.
希望能帮助到你。
';
return temp;
}
c