Yes, that's the one, thus it was caught in the catch (int) exception handler.You mean?
Yes, that's the one, thus it was caught in the catch (int) exception handler.You mean?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yeah, it is. I can't find the clause in the standard, but I'm only querying the 1997 draft, so that little detail may have been added later. However, Scott Meyers mentions it in Effective C++, in the item about operator new. (Item #8 2nd ed, Item #51 3rd ed)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
yes, it could already be used in MSVC6, although new would always return NULL in that version. They do it correct nowadays including nothrow
Just to avoid starting a new thread on a related subject, I was wondering whether
triggers undefined behavior even if p is not dereferenced. I'm guessing that the allocation of the underlying array is done in a similar manner to new[] and that the answer is no, but want to be sure. (This isn't exactly what my code does, but it may use the expression &v[0] even when v has size 0, though it never dereferences anything it's not supposed to.)Code:std::vector<T> v; T *p = &v[0];
Yes, it's undefined behaviour. Namely, it's v[0] that triggers it. That's a dereference right there.
Actually, with every sane standard library you'll get an assertion in debug mode.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Oops, I should have realized that. Thanks.
Edit: I've seen other code that does this:
Now I know why.Code:T *p = v.empty()? 0 : &v[0];
Last edited by robatino; 01-10-2008 at 05:38 PM.
If v was a dynamic array (in C or C++), I would have just used "v" instead of "&v[0]" to sidestep the problem. I wanted to avoid dynamic arrays, but my program needs a huge number of small arrays, all of the same length (length 1 would be common), and the container overhead of a vector for each one is unnecessary, so I allocate a single huge vector and pass out pieces of the underlying array instead. So memory management can still be done vector-style but the container overhead amortizes to almost zero.
Generally, "amortize" means to reduce to some lower value in the long run. In this context, robatino was trying to emphasize that in the long run, the overhead of a single vector is spread out over the many logical partitions that make up the small arrays needed. If multiple vectors was used instead, each vector would have its own container overhead.What do you mean "container overhead amortizes" and why it is zero?
As CornedBee pointed out, v[0] accesses an element that does not exist (since at that point the vector is empty). This results in undefined behaviour.I still do not know why "triggers undefined behavior even if p is not dereferenced" after reading the discussion from you guys. Why it is undefined? Could you help to provide more description please?
By the way, kindly attempt to address the various other users in a single post. I find that drafting a post with a text editor is useful for posting long posts on message boards.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thank you. I should also have mentioned that even if I allocated lots of little dynamic arrays, instead of vectors, there would still be per-object overhead, not to mention possible memory fragmentation. So it makes sense to allocate a single large container either way - and since the extra overhead of a huge vector vs. a huge dynamic array is negligible, it may as well be a vector.