5

Why is it workable? There are two different strings "testString" but the vector size is allocated correctly.

#include <iostream>
#include <vector>
#include <iterator>

int main()
{
    std::vector<char> str;
    str.assign(std::begin("testString"), std::end("testString"));
    copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout, " "));

    std::cout<<str.size();

    return 1;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
D. Alex
  • 167
  • 1
  • 1
  • 6
  • If you find any of the answers satisfying, you should [accept](http://meta.stackexchange.com/a/5235/225842) it. – Antonio Sep 21 '16 at 12:59

4 Answers4

9

You got lucky and compiler performed string pooling optimisation. Note that what you doing is still undefined behaviour and with different compiler or different compiler settings you will not be so lucky. Do not do that again.

Community
  • 1
  • 1
Revolver_Ocelot
  • 8,609
  • 3
  • 30
  • 48
2

The two identical literal strings have most likely been combined into a single one in memory so the begin and end refer to the same string.

This only works by 'accident' in your program though...

This is a somewhat related How Do C++ Compilers Merge Identical String Literals

Community
  • 1
  • 1
jcoder
  • 29,554
  • 19
  • 87
  • 130
2

Since the compiler is allowed to store the same string literal that is used in different location in one place more than likely both std::begin("testString") and std::end("testString") are actually referring to the same string.

This is not mandated to happen by the standard and the compiler could store both of those string literals in different locations which would break this code. Just because it is working does not mean you should do this. I would suggest using

std::vector<char> str;
const char [] chstr = "testString"
str.assign(std::begin(chstr), std::end(chstr));

Or better yet

std::string str = "testString";
copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout, " "));
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2

There are two different strings "testString"

They're the same in your case. The compiler is allowed to combine storage for equal or overlapping string literals.

The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.

You can check it by,

std::cout << std::boolalpha << ("testString"=="testString");

LIVE

Anyway, you shouldn't depend on it, the behavior is not guaranteed.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405