0

If I have a function

void x(std::string const& s)
{
   ...
}

And I am calling it as x("abc"), will string constructor allocate memory and copy data in it?

zzz777
  • 573
  • 2
  • 19
  • 5
    The only good answer here would be **[read a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)**, IMO – sehe May 14 '12 at 16:54
  • 2
    @Nathan: I think the question is not whether it's possible, but whether it's efficient (the [performance] tag confirms this) – Niklas B. May 14 '12 at 16:56
  • 1
    Worrying about the performance of copying a 4 byte string seems a bit ironic. To answer a performance related question with anything else than "try it" there would need to be much more context. – AJG85 May 14 '12 at 17:02
  • 1
    If are using C++11 passing by value may be faster – Captain Obvlious May 14 '12 at 17:06

4 Answers4

4

The std::string constructor will be called with a const char* argument

There is no telling whether memory would be allocated (dynamically), but the chances are that your standard library implementation has the SSO in place, which means it can store small strings without dynamic allocations.

SSO: Meaning of acronym SSO in the context of std::string

Community
  • 1
  • 1
sehe
  • 328,274
  • 43
  • 416
  • 565
  • I was talking about different implementation - we have static const object already and it would be enough to just calculate length and provide thin wrapper around it. This construct is being used so often that I simply could not believe that there are no targeted optimizations. – zzz777 May 14 '12 at 19:43
  • @zzz777 I suggest the obvious: make your constant `static const std::string ABC = "abc";`. – sehe May 14 '12 at 21:41
  • Nobody will bother doing it there are 100s of such things (e.g. string comparisons with c-strings). – zzz777 May 14 '12 at 23:08
  • @zzz777: you said you _already have_ a static const object. Make _that_ a std::string. If you pass it to your `void x(std::string const&)` you will **not** incur a copy, is the point. – sehe May 15 '12 at 06:53
2

The question is tagged with 'performance', so it's actually a good question IMO.

All compilers I know will allocate a copy of the string on the heap. However, some implementation could make the std::string type intrinsic into the compiler and optimize the heap allocation when an r-value std::string is constructed from a string literal.

E.g., this is not the case here, but MSVC is capable of replacing heap allocations with static objects when they are done as part of dynamic initialization of statics, at least in some circumstances.

Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
1

Yes, the compiler will generate the necessary code to create a std::string and pass it as argument to the x function.

mfontanini
  • 20,082
  • 3
  • 56
  • 73
  • I think the question was "will string constructor allocate memory and copy data in it?". The [performance] tag confirms that this is not about whether it's *possible*, but whether it's efficient. – Niklas B. May 14 '12 at 16:55
  • 1
    If you're gonna explain, at least explain that the compiler allocates a temporary and binds the parameter to it, because std::string has an _implicit conversion constructor_ that takes a `const char*` (and `const char[]` decays to that silently). The temporary lives till the end of the containing full-expression and will then be destructed, deterministically, like any automatic variable – sehe May 14 '12 at 16:56
0

Constructors which take a single argument, unless marked with the explicit keyword, are used to implicitly convert from the argument type to an instance of the object.

In this example, std::string has a constructor which takes a const char* argument, so the compiler uses this to implicitly convert your string literal into a std::string object. The const reference of that newly created object is then passed to your function.

Here's more information: What does the explicit keyword mean in C++?

Community
  • 1
  • 1
Matt Kline
  • 9,013
  • 3
  • 44
  • 76