undefined-behavior

Turning vector of shared_ptr into vector of shared_ptr to const

纵然是瞬间 提交于 2020-01-24 03:04:24
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal

How do I convert an arbitrary double to an integer while avoiding undefined behavior?

有些话、适合烂在心里 提交于 2020-01-22 13:38:27
问题 Let's say I've got a function that accepts a 64-bit integer, and I want to call it with a double with arbitrary numeric value (i.e. it may be very large in magnitude, or even infinite): void DoSomething(int64_t x); double d = [...]; DoSomething(d); Paragraph 1 of [conv.fpint] in the C++11 standard says this: A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion trun- cates; that is, the fractional part is discarded. The behavior is undefined if

How do I convert an arbitrary double to an integer while avoiding undefined behavior?

偶尔善良 提交于 2020-01-22 13:38:05
问题 Let's say I've got a function that accepts a 64-bit integer, and I want to call it with a double with arbitrary numeric value (i.e. it may be very large in magnitude, or even infinite): void DoSomething(int64_t x); double d = [...]; DoSomething(d); Paragraph 1 of [conv.fpint] in the C++11 standard says this: A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion trun- cates; that is, the fractional part is discarded. The behavior is undefined if

Is it UB to Modify where pointer points, when original data is const?

喜你入骨 提交于 2020-01-22 02:49:07
问题 Would it be undefined behavior to change where a pointer points, when its data is const? Example: const char* p = "foo"; p = "boo"; I believe that this is not UB, because the pointer itself is not const and I'm not modifying the "foo" object. Extra question: and altering not const data of a const pointer? Would be UB? Example: char* const p = "foo"; (*(char**)&p) = (char*)malloc(strlen(p)); 回答1: I believe that this is not UB, because the pointer itself is not const and I'm not modifying the

Why is this not a memory leak in C++?

强颜欢笑 提交于 2020-01-21 12:21:45
问题 A couple of months ago I asked this question where I asked why there was a memory leak. Apparently, I forgot a virtual destructor. Now I'm struggling to understand why this is not a memory leak: #include <iostream> #include <vector> #include <memory> using namespace std; class Base{ public: explicit Base(double a){ a_ = a; } virtual void fun(){ cout << "Base " << a_ << endl; } protected: double a_; }; class Derived : public Base{ public: Derived(double a, double b): Base(a), b_{b}{ } void fun

Is using const_cast for read-only access to a const object allowed?

不问归期 提交于 2020-01-19 16:22:19
问题 In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result; } and I need to call it for a const array: static const int Array[] = { 10, 20, 0, 2}; countZeroes( const_cast<int*>( Array ), sizeof( Array ) / sizeof( Array[0] ) ); will this be undefined behaviour? If so

C - is an indeterminate value indeterminable?

白昼怎懂夜的黑 提交于 2020-01-19 03:51:09
问题 According to this post an indeterminate value is: 3.17.2 1 indeterminate value either an unspecified value or a trap representation According to google, the definition of indeterminate is: Not certain, known, or established Left doubtful; vague. According to thefreedictionary, determinable is: capable of being determined According to merriam-webster, to determine (in the particular context) is: to find out or come to a decision about by investigation, reasoning, or calculation So, common

Is it undefined behavior to do runtime borrow management with the help of raw pointers in Rust?

…衆ロ難τιáo~ 提交于 2020-01-14 19:50:51
问题 As part of binding a C API to Rust, I have a mutable reference ph: &mut Ph , a struct struct EnsureValidContext<'a> { ph: &'a mut Ph } , and some methods: impl Ph { pub fn print(&mut self, s: &str) { /*...*/ } pub fn with_context<F, R>(&mut self, ctx: &Context, f: F) -> Result<R, InvalidContextError> where F: Fn(EnsureValidContext) -> R, { /*...*/ } /* some others */ } impl<'a> EnsureValidContext<'a> { pub fn print(&mut self, s: &str) { self.ph.print(s) } pub fn close(self) {} /* some others

Why do STL-Datastructures need fully defined types

我只是一个虾纸丫 提交于 2020-01-14 09:05:30
问题 When looking for a solution to this question, I found the this thread on another forum, which says that the standard requires all template parameters to STL-Datastructure to be fully defined. This means producing a structure which stores elements of the it's own type within itself invokes undefined behavior. However as far as I can tell this is not caught for most pre-C++11 datastructure (i.e. std::vector , std::map etc.). What actually could be the problem of using incomplete types within

Threshold an absolute value

白昼怎懂夜的黑 提交于 2020-01-14 08:50:29
问题 I have the following function: char f1( int a, unsigned b ) { return abs(a) <= b; } For execution speed, I want to rewrite it as follows: char f2( int a, unsigned b ) { return (unsigned)(a+b) <= 2*b; } // redundant cast Or alternatively with this signature that could have subtle implications even for non-negative b : char f3( int a, int b ) { return (unsigned)(a+b) <= 2*b; } Both of these alternatives work under a simple test on one platform, but I need it to portable. Assuming non-negative b