Constant strings address

自古美人都是妖i 提交于 2019-12-10 13:15:27

问题


I have several identical string constants in my program:

const char* Ok()
{
  return "Ok";  
}

int main()
{
  const char* ok = "Ok";
}

Is there guarantee that they are have the same address, i.e. could I write the following code? I heard that GNU C++ optimize strings so they have the same address, could I use that feature in my programs?

int main()
{
  const char* ok = "Ok";
  if ( ok == Ok() ) // is it ok?
  ;
}

回答1:


There's certainly no guarantee, but it is a common (I think) optimization.

The C++ standard says (2.13.4/2 "String literals):

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined.

To be clear, you shouldn't write code that assumes this optimization will take place - as Chris Lutz says, C++ code that relies on this is code that's waiting to be broken.




回答2:


this is called string interning

In you case it is better not to rely on that. The scopes are different, but I don't find myself very competent on this subject




回答3:


GCC uses such optimization, and Microsoft does (they call it string pooling). It is just optimization, C++ Standard explicitly states that you cannot use that (in 2.13.4/2). In addition, just imagine that you'll get pointer to string from some other module/library - I don't think compiler could make such optimization in that case.




回答4:


Is there guarantee that they are have the same address, i.e. could I write the following code?

The standard allows such optimizations since string literals are read-only.

I heard that GNU C++ optimize strings so they have the same address, could I use that feature in my programs?

Yes, GCC/G++ often do that. AFAIK, there's an option to turn this on/off.




回答5:


There's no such guarantee. The language just says that they might have the same address. Or they might not.




回答6:


Actually, there is a solution, so simple solution:

char const * const Message_Ok = "OK";

char const * const OK() { return Message_Ok; }

int main(int argc, const char* argv[])
{
  if (OK() == Message_Ok) { std::cout << "OK" << std::endl; }
  return 0;
}

You cannot compare two different string literals, but use a const global variable to convey your meaning and it's OK to compare the memory address :)

Some extern may be missing... I've got some difficulty with that beast



来源:https://stackoverflow.com/questions/1611673/constant-strings-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!