undefined-behavior

Mixing class and struct

耗尽温柔 提交于 2019-12-28 01:20:10
问题 I'm well aware of the difference between class and struct, however I'm struggling to authoritatively say if this is well defined: // declare foo (struct) struct foo; // define foo (class) class foo { }; // instance of foo, claiming to be a struct again! Well defined? struct foo bar; // mixing class and struct like this upsets at least one compiler (names are mangled differently) const foo& test() { return bar; } int main() { test(); return 0; } If this is undefined behaviour can someone point

So why is i = ++i + 1 well-defined in C++11?

杀马特。学长 韩版系。学妹 提交于 2019-12-27 22:06:26
问题 I've seen the other similar questions and read the defect about it. But I still don't get it. Why is i = ++i + 1 well-defined in C++11 when i = i++ + 1 is not? How does the standard make this well defined? By my working out, I have the following sequenced before graph (where an arrow represents the sequenced before relationship and everything is a value computation unless otherwise specified): i = ++i + 1 ^ | assignment (side effect on i) ^ ^ | | ☆i ++i + 1 || ^ i+=1 | ^ 1 | ★assignment (side

So why is i = ++i + 1 well-defined in C++11?

别等时光非礼了梦想. 提交于 2019-12-27 22:04:32
问题 I've seen the other similar questions and read the defect about it. But I still don't get it. Why is i = ++i + 1 well-defined in C++11 when i = i++ + 1 is not? How does the standard make this well defined? By my working out, I have the following sequenced before graph (where an arrow represents the sequenced before relationship and everything is a value computation unless otherwise specified): i = ++i + 1 ^ | assignment (side effect on i) ^ ^ | | ☆i ++i + 1 || ^ i+=1 | ^ 1 | ★assignment (side

Is right shift undefined behavior if the count is larger than the width of the type?

余生颓废 提交于 2019-12-27 11:52:15
问题 I just checked the C++ standard. It seems the following code should NOT be undefined behavior: unsigned int val = 0x0FFFFFFF; unsigned int res = val >> 34; // res should be 0 by C++ standard, // but GCC gives warning and res is 67108863 And from the standard: The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2^E2. If E1 has a signed type

Is right shift undefined behavior if the count is larger than the width of the type?

孤街醉人 提交于 2019-12-27 11:48:10
问题 I just checked the C++ standard. It seems the following code should NOT be undefined behavior: unsigned int val = 0x0FFFFFFF; unsigned int res = val >> 34; // res should be 0 by C++ standard, // but GCC gives warning and res is 67108863 And from the standard: The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2^E2. If E1 has a signed type

A question about union in C - store as one type and read as another - is it implementation defined?

纵然是瞬间 提交于 2019-12-27 11:14:34
问题 I was reading about union in C from K&R, as far as I understood, a single variable in union can hold any one of the several types and if something is stored as one type and extracted as another the result is purely implementation defined. Now please check this code snippet: #include<stdio.h> int main(void) { union a { int i; char ch[2]; }; union a u; u.ch[0] = 3; u.ch[1] = 2; printf("%d %d %d\n", u.ch[0], u.ch[1], u.i); return 0; } Output: 3 2 515 Here I am assigning values in the u.ch but

Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

偶尔善良 提交于 2019-12-26 06:49:13
问题 Suppose I have allocated some memory for storing an int value like this: int *p=new int; Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block. Now it's my control to what I store in that memory block. But when I write a statement like this: delete p; we say that I have deleted the dynamically allocated memory. But if I really delete 'd or freed up that memory, should I not be able to access that memory

Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

こ雲淡風輕ζ 提交于 2019-12-26 06:49:07
问题 Suppose I have allocated some memory for storing an int value like this: int *p=new int; Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block. Now it's my control to what I store in that memory block. But when I write a statement like this: delete p; we say that I have deleted the dynamically allocated memory. But if I really delete 'd or freed up that memory, should I not be able to access that memory

Pre increment and post increment function call

随声附和 提交于 2019-12-25 17:39:27
问题 #include<stdio.h> int main() { void add(); int i=2; add(i++,--i); print("%d",i) } void add(int a,int b) { print("%d %d",a,b); } /*what are a and b's value i am actually not getting the answer why b is 2 */ 回答1: in line 6 where add() is called first args is i++ so it will send value 2 ie value of i to the function and then add 1 now i=3. second args is --i now it will subtract 1 and iwill now be 2 again and then send value 2 to function So I think your answer will print 2 2 (that is value of a

Pre increment and post increment function call

北慕城南 提交于 2019-12-25 17:38:14
问题 #include<stdio.h> int main() { void add(); int i=2; add(i++,--i); print("%d",i) } void add(int a,int b) { print("%d %d",a,b); } /*what are a and b's value i am actually not getting the answer why b is 2 */ 回答1: in line 6 where add() is called first args is i++ so it will send value 2 ie value of i to the function and then add 1 now i=3. second args is --i now it will subtract 1 and iwill now be 2 again and then send value 2 to function So I think your answer will print 2 2 (that is value of a