reinterpret-cast

Is %p specifier only for valid pointers?

六眼飞鱼酱① 提交于 2019-12-09 08:18:48
问题 Suppose on my platform sizeof(int)==sizeof(void*) and I have this code: printf( "%p", rand() ); Will this be undefined behavior because of passing a value that is not a valid pointer in place of %p ? 回答1: To expand upon @larsman's answer (which says that since you violated a constraint, the behavior is undefined), here's an actual C implementation where sizeof(int) == sizeof(void*) , yet the code is not equivalent to printf( "%p", (void*)rand() ); The Motorola 68000 processor has 16 registers

Is the std::array bit compatible with the old C array?

混江龙づ霸主 提交于 2019-12-09 07:30:34
问题 Is the underlying bit representation for an std::array<T,N> v and a T u[N] the same? In other words, is it safe to copy N*sizeof(T) bytes from one to the other? (Either through reinterpret_cast or memcpy .) Edit: For clarification, the emphasis is on same bit representation and reinterpret_cast . For example, let's suppose I have these two classes over some trivially copyable type T , for some N : struct VecNew { std::array<T,N> v; }; struct VecOld { T v[N]; }; And there is the legacy

Is reinterpret_cast mostly useless?

一世执手 提交于 2019-12-09 05:14:24
问题 I've read various previous questions about the use of reinterpret_cast, and I've also read the relevant wording in the C++ standard. Essentially, what it comes down to is that the result of a pointer-to-pointer reinterpret_cast operation can't safely be used for anything other than being cast back to the original pointer type. In practice, however, most real-world uses of reinterpret_cast seem to be based on the (wrong) assumption that a reinterpret_cast is the same as a C-style cast. For

Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast

雨燕双飞 提交于 2019-12-08 18:35:15
问题 So if your converting from Void* to Type* or from Type* to Void* should you use: void func(void *p) { Params *params = static_cast<Params*>(p); } or void func(void *p) { Params *params = reinterpret_cast<Params*>(p); } To me static_cast seems the more correct but I've seen both used for the same purpose. Also, does the direction of the conversion matter. i.e. should I still use static_cast for: _beginthread(func,0,static_cast<void*>(params) I have read the other questions on C++ style casting

reinterpret_cast behavior when dereferencing a void pointer

青春壹個敷衍的年華 提交于 2019-12-07 17:47:14
问题 While arguing with someone over the suggestion he made in the comment thread of this answer, I came across some code that gcc4.8 and VS2013 refuse to compile but clang happily accepts it and displays the correct result. #include <iostream> int main() { int i{ 5 }; void* v = &i; std::cout << reinterpret_cast<int&>(*v) << std::endl; } Live demo. Both GCC and VC fail with the error I was expecting, complaining that the code attempts to dereference a void* within the reinterpret_cast . So I

Is reinterpret_cast to C-style array illegal C++11?

末鹿安然 提交于 2019-12-07 13:32:18
问题 Why is the following code frowned upon? double d[4] = {0,1,2,3}; reinterpret_cast<double[2]>(d); GCC declares it an invalid cast from type 'double*' to type 'double [2]' and clang declares that reinterpret_cast from 'double *' to 'double [2]' is not allowed Now in case the intent is not obvious, I would like this code to return a double[2] that contains {0,1}, pretty much like a reinterpret_cast<double*>(d) would. (Hence I know it would work with pointers, so that's not what I'm asking) 回答1:

Why is it allowed to reinterpret_cast integral, enumeration and pointer-to-member types to themselves?

风流意气都作罢 提交于 2019-12-07 03:25:12
问题 In this recent question, we saw that it's not allowed to reinterpret_cast some custom class type instance to itself; struct A{}; reinterpret_cast<A>(A{}); is invalid (it works only through references or pointers). Which seems to make sense, because of the lack of real-world scenarios where such identity conversion is necessary. Checking the corresponding standard clause, we have in [expr.reinterpret.cast] (emphasis mine): 1 [...] Conversions that can be performed explicitly using reinterpret_

how convert std::array<char, N> to char (&dest)[N]?

做~自己de王妃 提交于 2019-12-07 02:40:55
问题 What is the way to pass std::array<char, N> to such function: template<size_t N> void safe_func(char (&dest)[N]); ? I try this one: #include <array> template <size_t N> using SafeArray = char[N]; template <size_t N> void safe_func(char (&dest)[N]) {} int main() { SafeArray<10> a1; safe_func(a1); std::array<char, 10> a2; safe_func(*static_cast<SafeArray<10> *>(static_cast<void *>(a2.data()))); } It works, but I doubt, may be something wrong with my cast, and on other compiler or platform (I

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

拜拜、爱过 提交于 2019-12-06 20:20:34
问题 I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast<void ** const>(ppFred); Please could someone explain why a reinterpret_cast is needed to convert a pointer to Fred* to a pointer to void* but static_cast is fine to convert pointer to Fred to a pointer to void . 回答1: There's no requirement that a Fred* and a void* have the same size

reinterpret_cast vs strict aliasing

半腔热情 提交于 2019-12-05 21:34:20
问题 I was reading about strict aliasing, but its still kinda foggy and I am never sure where is the line of defined / undefined behaviour. The most detailed post i found concentrates on C. So it would be nice if you could tell me if this is allowed and what has changed since C++98/11/... #include <iostream> #include <cstring> template <typename T> T transform(T t); struct my_buffer { char data[128]; unsigned pos; my_buffer() : pos(0) {} void rewind() { pos = 0; } template <typename T> void push