language-lawyer

Is casting a pointer to const pointer and cast back to the original type undefined?

吃可爱长大的小学妹 提交于 2021-01-29 02:24:16
问题 I know casting a const pointer to non-const type might be undefined behavior, but what if the pointer is originally not const? int i = 0; int * pi = &i; const int * const_pi = const_cast<const int*>(pi); int * non_const_pi = const_cast<int*>(const_pi); *non_const_pi = 0; *non_const_pi = 1; int j = *non_const_pi; Is there's any undefined behavior? If any, where do they happen? May the compiler assume that non_const_pi is casted from a const pointer and perform no modification? 回答1: I know

Is casting a pointer to const pointer and cast back to the original type undefined?

China☆狼群 提交于 2021-01-29 02:21:30
问题 I know casting a const pointer to non-const type might be undefined behavior, but what if the pointer is originally not const? int i = 0; int * pi = &i; const int * const_pi = const_cast<const int*>(pi); int * non_const_pi = const_cast<int*>(const_pi); *non_const_pi = 0; *non_const_pi = 1; int j = *non_const_pi; Is there's any undefined behavior? If any, where do they happen? May the compiler assume that non_const_pi is casted from a const pointer and perform no modification? 回答1: I know

What rules are there for qualifiers of effective type?

不想你离开。 提交于 2021-01-28 11:32:51
问题 So I was re-reading C17 6.5/6 - 6.5/7 regarding effective type and strict aliasing, but couldn't figure out how to treat qualifiers. Some things confuse me: I always assumed that qualifiers aren't really relevant for effective type since the rules speak of lvalue access, meaning lvalue conversion that discards qualifiers. But what if the object is a pointer? Qualifiers to the pointed-at data aren't affected by lvalue conversion. Q1: What if the effective type is a pointer to qualified-type?

P1359R0 [basic.lval] wording change and aliasing rules [duplicate]

一世执手 提交于 2021-01-28 10:59:56
问题 This question already has an answer here : What happened to the “aggregate or union type that includes one of the aforementioned types” strict aliasing rule? (1 answer) Closed last year . The N4778 draft (2018) of the C++ standard contains the following section: 7.2.1 [basic.lval] If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined: the dynamic type of the object , a cv-qualified version of the

Does sort -n handle ties predictably when the --stable option is NOT provided? If it does, how?

烈酒焚心 提交于 2021-01-28 04:50:48
问题 Here it looks like the space after the 3 in both rows breaks the numerical sorting and lets the alphabetic sorting kick in, so that 11 < 2 : $ echo -e '3 2\n3 11' | sort -n 3 11 3 2 In man sort , I read -s, --stable stabilize sort by disabling last-resort comparison which implies that without -s a last-resort comparison is done (between ties, because -s does not affect non-ties). So the question is: how is this last-resort comparison accomplished? A reference to the source code would be

alignas() effect on sizeof() - mandatory?

女生的网名这么多〃 提交于 2021-01-27 15:55:44
问题 This program: struct alignas(4) foo {}; int main() { return sizeof(foo); } returns 4, with GCC 10.1 and clang 10.1, and icc 19.0.1 . That makes me wonder - is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at? Or - is this change just the implementation's prerogative? 回答1: is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at?

alignas() effect on sizeof() - mandatory?

此生再无相见时 提交于 2021-01-27 15:40:42
问题 This program: struct alignas(4) foo {}; int main() { return sizeof(foo); } returns 4, with GCC 10.1 and clang 10.1, and icc 19.0.1 . That makes me wonder - is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at? Or - is this change just the implementation's prerogative? 回答1: is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at?

In c++ how to check a pointer lies within a range?

房东的猫 提交于 2021-01-27 13:53:03
问题 Intuitively to check whecker pointer p lies in [ a , b ) one will do a<=p && p<b However, comparing pointers from two arrays results in unspecified behavior and thus we cannot safely say p is in [ a , b ) from this comparison. Is there any way one can check for this with certainty? (It would be better if it can be done for std::vector<T>::const_iterator , but I don't think it's feasible.) 回答1: Here's a partial solution. You can leverage the fact that the comparison would invoke unspecified

std::vector<std::wstring> Is moving/reallocating inner wstring.data() legal?

夙愿已清 提交于 2021-01-27 13:31:40
问题 Here is an excerpt: ... std::vector<std::wstring> vecWstr; vecWstr.emplace_back(L"1"); wchar_t* data1 = vecWstr[0].data(); //<-This pointer needed for future use. vecWstr.emplace_back(L"2"); wchar_t* data2 = vecWstr[0].data(); if (data1 != data2) MessageBox(L"Error, not equal.", L"Compare"); MessageBox always arises. So, here i compare two wstring buffers before and after .emplace() . In my understanding they must be equal. The main concern here is: Why vector moves/reallocates 1st innner std

std::vector<std::wstring> Is moving/reallocating inner wstring.data() legal?

不打扰是莪最后的温柔 提交于 2021-01-27 13:26:06
问题 Here is an excerpt: ... std::vector<std::wstring> vecWstr; vecWstr.emplace_back(L"1"); wchar_t* data1 = vecWstr[0].data(); //<-This pointer needed for future use. vecWstr.emplace_back(L"2"); wchar_t* data2 = vecWstr[0].data(); if (data1 != data2) MessageBox(L"Error, not equal.", L"Compare"); MessageBox always arises. So, here i compare two wstring buffers before and after .emplace() . In my understanding they must be equal. The main concern here is: Why vector moves/reallocates 1st innner std