简体   繁体   English

如何将本地std :: string复制到const char *?

[英]How to copy local std::string to const char *?

const char* getString() {
  std::string myString = "Hello!";
  return myString.str().c_str();
}

How can I get this function to return a const char * which will live past the local scope in which it is declared? 如何让这个函数返回一个const char * ,它将超过声明它的本地范围? I believe I need to use malloc , but I'm not sure. 我相信我需要使用malloc ,但我不确定。

我想你需要一个string s的静态查找表,以便SymbolLookUp可以返回c_str值。

It looks like SymbolLookup API you're hooking into is assuming that the symbols are stored in a quasi-permanent structure and not generated on the fly. 它看起来像你正在挂钩的SymbolLookup API假设符号存储在准永久结构中而不是动态生成。 If your symbols are really transient, you need to make them permanent in some way, either C style through something like 如果你的符号非常短暂,你需要以某种方式使它们永久化,或者通过类似的方式使它成为C样式

return strdup(myString.c_str());

or in a more idiomatic C++ style with: 或者用更加惯用的C ++风格:

static std::vector<std::string> sStringPool;
sStringPool.push_back(myString);
return sStringPool.back().c_str();

Naturally, this is going to lead to unbounded memory growth, but if you have no other information about string lifetimes, there are few alternatives. 当然,这将导致无限的内存增长,但如果你没有关于字符串生命周期的其他信息,那么几乎没有其他选择。 If you want to get clever, you can at least unique the strings: 如果你想变得聪明,你至少可以使字符串独一无二:

static std::set<std::string> sStringPool;
return sStringPool.insert(sStringPool.end(), myString)->c_str();

Simply return the std::string and keep using it. 只需返回std::string并继续使用它。

Just be careful about its lifetime, for example 例如,请注意其寿命

const char* s = getString().c_str(); doStuff(s);

will cause the second statement to fail because the temporary string returned by getString only lives until the end of the statement. 将导致第二个语句失败,因为getString返回的临时字符串仅存在于语句结束之前。

If the API you are using takes a const char* , the following would work: 如果您使用的API采用const char* ,则以下内容将起作用:

callTheAPI(getString().c_str());

and that string would then live for until the API returns. 然后该字符串将一直存在直到API返回。

EDIT: note, that it answers the question as it was stated. 编辑:请注意,它回答了问题。 At time of this edit it seem obvious the question is misstated wrt original intent, but improved answer can't be provided until consolidation, if it's possible at all.) 在编辑时,似乎显而易见的是,这个问题与原始意图有误,但是如果可能的话,在合并之前不能提供改进的答案。)

Just don't do it. 只是不要这样做。 The whole point of using string class is to not use malloc and whatever crap. 使用字符串类的重点是不使用malloc和任何废话。 If you can't provide a const char* to a suitably stable existing location, return std::string from the function (or some other string class matching your taste). 如果你不能将const char*提供给一个适当稳定的现有位置,则从函数中返回std :: string(或者根据你的喜好返回一些其他字符串类)。

const char* getString() {
  std::string myString = "Hello!";
  return myString.str().c_str();
}

1) The correct syntax is myString.c_str() 1)正确的语法是myString.c_str()

2) That function returns a string on stack, so when you return such data, the compiler either makes a copy of it or does some optimization 2)该函数在堆栈上返回一个字符串,因此当您返回此类数据时,编译器会复制它或进行一些优化

3) Note that it returns const char * that means the correct way to bind that return value is to have statement such as const char *returnValue = getString() or when you pass that into a function that expects a const char. 3)请注意,它返回const char * ,这意味着绑定该返回值的正确方法是使用诸如const char *returnValue = getString()或者将它传递给需要const char的函数。 And because we are assigning a const char * to the returnValue, from C++ spec, that data will live passed the lifetime of the function. 而且因为我们正在将一个const char *分配给来自C ++规范的returnValue,所以该数据将在该函数的生命周期内传递。 So that means we can do const char *str = getString() where getString is your function and it is defined and that the value returned by getString() lives passed the function's lifetime. 这意味着我们可以执行const char *str = getString() ,其中getString是你的函数,它被定义并且getString()返回的值传递给函数的生命周期。

I don't see any necessary problem with the code you've posted above. 我发现您上面发布的代码没有任何必要的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM