strict-aliasing

Placement new base subobject of derived in C++

断了今生、忘了曾经 提交于 2020-07-09 08:26:04
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

Placement new base subobject of derived in C++

橙三吉。 提交于 2020-07-09 08:23:05
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

Effective type rules with relation to strict aliasing

徘徊边缘 提交于 2020-05-15 09:32:25
问题 So, I've been banging my head against the Strict Aliasing Rule and the effective type rules for the past couple of days. While the spirit of it is pretty clear, I'd like to nail down a good technical understanding of the rules. Please note I've gone through many related questions on SO, but I don't feel that the questions to be presented here have been answered in a way that really sits with me in any other place. This question is divided into two parts. In the first part, I divide the

Methods to convert `void *` function parmeter inconsistant from type to type

人走茶凉 提交于 2020-02-22 19:21:55
问题 Note: This question attempts to improve what I attempted to ask here, but fell short. Also, I have seen this, and this. They discuss similar concepts, but do not answer these questions. My environment is Windows 10, and for testing I used two compilers, CLANG and GCC. I am passing variables via a void * function argument, and need to convert them. I would like to get some feedback on inconsistencies I am seeing between methods for different types. The following is a stripped-down depiction of

Methods to convert `void *` function parmeter inconsistant from type to type

末鹿安然 提交于 2020-02-22 19:20:11
问题 Note: This question attempts to improve what I attempted to ask here, but fell short. Also, I have seen this, and this. They discuss similar concepts, but do not answer these questions. My environment is Windows 10, and for testing I used two compilers, CLANG and GCC. I am passing variables via a void * function argument, and need to convert them. I would like to get some feedback on inconsistencies I am seeing between methods for different types. The following is a stripped-down depiction of

Method to pass double to void * argument

白昼怎懂夜的黑 提交于 2020-01-23 16:42:46
问题 My question is driven by the need to correctly pass a double variable via a void * function argument. I ask because on my machine sizeof(void *) is 4 , and sizeof(double) is 8 , casting one to the other seems like it should result in a problem, but my compiler (CLANG, with ALL warnings on.) gives no indication of a problem, and the code seems to work fine. Note that I have seen this, and this. They have similar component words in their titles, but do not answer this specific question. Would

Method to pass double to void * argument

荒凉一梦 提交于 2020-01-23 16:42:29
问题 My question is driven by the need to correctly pass a double variable via a void * function argument. I ask because on my machine sizeof(void *) is 4 , and sizeof(double) is 8 , casting one to the other seems like it should result in a problem, but my compiler (CLANG, with ALL warnings on.) gives no indication of a problem, and the code seems to work fine. Note that I have seen this, and this. They have similar component words in their titles, but do not answer this specific question. Would

(How) am I supposed to destroy a uv_async_t?

丶灬走出姿态 提交于 2020-01-15 03:35:50
问题 After I'm done with a uv_async_t , I'm supposed to destroy it to avoid any leaks, right? From glancing at the docs, it appears I'm supposed to use uv_close() for this, but it takes a uv_handle_t* , not a uv_async_t* . Furthermore, it looks like casting it (as in uv_close((uv_handle_t *)async, NULL) ) would cause a strict aliasing violation. Is that what I'm supposed to do anyway? 回答1: Yes, you have to cast the uv_async_t* to uv_handle_t* . That's how libuv internally works. All handles share