由于我无法返回局部变量,从 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?

查看:16
本文介绍了由于我无法返回局部变量,从 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;
}

如果我是对的,修复第二个问候功能的最佳方法是什么?在嵌入式环境中?在桌面上?

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

推荐答案

你说得对.第二个示例中的 c 数组正在堆栈上分配,因此内存将立即被重用.特别是,如果你有像

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
",greet());

你会得到奇怪的结果,因为对 printf 的调用会重用数组的一些空间.

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

解决方案是在其他地方分配内存.例如:

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.

似乎比我预期的更令人困惑的事情之一就是内存泄漏"到底是什么?是.泄漏是当您动态分配内存但丢失地址因此无法释放时.这些示例中没有一个一定存在泄漏,但只有第三个甚至可能存在泄漏,因为它是唯一一个动态分配内存的示例.因此,假设第三个实现,您可以编写以下代码:

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
", 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
", cp);
    free(cp);
}

不会泄漏,因为指针保存在一个自动变量 cp 中足够长以调用 free() .现在,即使 cp 在执行结束后立即消失,但由于调用了 free,因此内存被回收并且没有泄漏.

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天全站免登陆