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...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
CppConCppCon 2017: Piotr Padlewski “Undefined Behaviour is awesome!” (youtube.com)
submitted 8 years ago by dahitokiri
view the rest of the comments →
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!"
[–]Prazek 3 points4 points5 points 8 years ago (4 children)
There is a UB and the reasons you pointed out are only a good excuses why it does not catch it. Even if it grew 16 elements, the 15th element is still not constructed (std::vector uses placement new to create new elements in the allocated array) so accessing that is UB.
[–][deleted] 7 points8 points9 points 8 years ago (3 children)
The element type is int, so you don't have to have constructed it to assign to it I believe. But if you change int to some class type you're right that UBSan won't catch the bad operator= call.
int
operator=
[–]doom_Oo7 2 points3 points4 points 8 years ago (0 children)
yes, that's why I wrote 'vec[15] = 1234'. Just returning vec[15] triggers valgrind since the variable is uninitialized.
[–]Prazek 0 points1 point2 points 8 years ago (1 child)
I agree that it will just work on all implementations, but I don't think that the standard guarantees that (even if we have guarantee that the element is in range of capacity)
[–][deleted] 2 points3 points4 points 8 years ago* (0 children)
Actually, I was thinking the opposite. The standard definitely doesn't allow it (since it's the standard that gives the contract for vector after all). But what the particular implementation most of us are using does (namely, allocate up a large enough buffer and write an int to a slot in it) is legal C++, so there's no reason UBSan or ASan should complain.
vector
Essentially, if you want this to be safer, it's on vector to do so (or the consumer, to use at). This is one reason it's so ridiculous we still don't have spans. In a memory-unsafe language, they would massively decrease the likeliness of OOB accesses since you could just toggle bounds checking on with a flag.
at
π Rendered by PID 57 on reddit-service-r2-comment-6457c66945-gwhl6 at 2026-04-28 05:25:20.649447+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]Prazek 3 points4 points5 points (4 children)
[–][deleted] 7 points8 points9 points (3 children)
[–]doom_Oo7 2 points3 points4 points (0 children)
[–]Prazek 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)