既然不能返回一个局部变量,什么是从C或C ++函数返回一个字符串的最好方法? [英] Since I can't return a local variable, what's the best way to return a string from a C or C++ function?

查看:167
本文介绍了既然不能返回一个局部变量,什么是从C或C ++函数返回一个字符串的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为后续这个问题

这是我所看到的,这应该按预期工作:

From what I've seen, this should work as expected:

void greet(){
  char c[] = "Hello";
  greetWith(c);
  return;
}

但这样会导致未定义的行为:

but this will cause undefined behavior:

char *greet(){ 
  char c[] = "Hello";
  return c;
}

如果我是正确的,什么是解决第二greet函数的最好方法?在嵌入式环境中?在桌面?

If I'm right, what's the best way to fix the second greet function? In an embedded environment? On a desktop?

推荐答案

您是绝对正确的。在第二个例子中你的C数组被分配在栈上,因此内存将立即得到重用以下。特别是,如果你有code像

You're absolutely right. Your c array in the second example is being allocated on the stack, and thus the memory will get reused immediately following. In particular, if you had code like

 printf("%s\n",greet());

你会得到奇怪的结果,因为调用的printf会重用你的一些阵列的空间。

you'd get weird results, because the call to printf would have reused some of the space of your array.

的解决方案是在其他地方分配的存储器。对于expample:

The solution is to allocate the memory somewhere else. For expample:

char c[] = "Hello";

char * greet() {
    return c;
}

会工作。另一种选择是在范围上静态分配的:

Would work. Another choice would be to allocate it statically in scope:

char * greet() {
    static char c[] = "Hello";
    return c;
}

,因为静态存储器是分开在数据空间中的堆栈分配的

because static memory is allocated separately from the stack in data space.

您的第三选择是分配它通过的malloc堆:

Your third choice is to allocate it on the heap via malloc:

char * greet() {
   char * c = (char *) malloc(strlen("Hello")+1);  /* +1 for the null */
   strcpy(c, "Hello");
   return c;
}

但现在你必须确保内存被释放不知何故,否则你有内存泄漏。

but now you have to make sure that memory is freed somehow, or else you have a memory leak.

一,这似乎更混乱比我期望那些东西究竟是什么一个内存泄漏的说法。一,泄漏是当你动态分配内存,但失去了地址,所以它不能被释放。这些例子都没有必然有泄漏,但只有第三个甚至可能有泄漏,因为它是动态分配内存的唯一的一个。因此,假设第三个执行中,你可以写这个code:

One of those things that seems more confusing than I expect is what exactly a "memory leak" is. A leak is when you allocate memory dynamically, but lose the address so it can't be freed. None of these examples necessarily has a leak, but only the third one even potentially has a leak, because it's the only one that allocates memory dynamically. So, assuming the third implementation, you could write this code:

{
    /* stuff happens */
    printf("%s\n", greet());
}

这有泄漏;的指针malloc分配内存返回时,的printf 并使用它,那么它的丢失;你不能再释放它。另一方面,

This has a leak; the pointer to the malloc'ed memory is returned, the printf uses it, and then it's lost; you can't free it any longer. On the other hand,

{
    char * cp ;
    /* stuff happens */
    cp = greet();
    printf("%s\n", cp);
    free(cp);
}

的泄漏,因为指针保存在一个自动变量 CP 足够长的时间来调用免费( )就可以了。现在,通过执行结束,他梅开二度,因为自由被调用,内存被回收,并没有泄漏,即使CP尽快消失。

doesn't leak, because the pointer is saved in an auto variable cp long enough to call free() on it. Now, even though cp disappears as soon as execution passes he end brace, since free has been called, the memory is reclaimed and didn't leak.

这篇关于既然不能返回一个局部变量,什么是从C或C ++函数返回一个字符串的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆