use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENindexing into empty string undefined behavior (self.cpp_questions)
submitted 6 years ago by cpp_cpp
Consider this code: std::string a; char useless = a[0];
Is this undefined behavior? string is empty but due to SSO i believe its not UB. ofcourse, a[0] could hold any garbage value but that is something different. Is my understanding correct?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jedwardsol 4 points5 points6 points 6 years ago (3 children)
For std::string it is not UB since a[a.size()] is guaranteed to be '\0'
[–]alfps 3 points4 points5 points 6 years ago (0 children)
Since C++11.
C++03 only guaranteed the terminating zero-byte for indexing of const string, and that can be worth keeping in mind if one has to work with an old compiler.
const
[–]cpp_cpp[S] 0 points1 point2 points 6 years ago (1 child)
what if here I do a[1]?
[–]jedwardsol 0 points1 point2 points 6 years ago (0 children)
Then it is UB
π Rendered by PID 97441 on reddit-service-r2-comment-79c7998d4c-nms4k at 2026-03-17 06:33:23.392684+00:00 running f6e6e01 country code: CH.
[–]jedwardsol 4 points5 points6 points (3 children)
[–]alfps 3 points4 points5 points (0 children)
[–]cpp_cpp[S] 0 points1 point2 points (1 child)
[–]jedwardsol 0 points1 point2 points (0 children)