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;
}
1
  • If you find any of the answers satisfying, you should accept it. Commented Sep 21, 2016 at 12:59

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.