undefined-behavior

Undefined behaviour with non-virtual destructors - is it a real-world issue?

别等时光非礼了梦想. 提交于 2019-12-10 17:32:49
问题 Consider the following code: class A { public: A() {} ~A() {} }; class B: public A { B() {} ~B() {} }; A* b = new B; delete b; // undefined behaviour My understanding is that the C++ standard says that deleting b is undefined behaviour - ie, anything could happen. But, in the real world, my experience is that ~A() is always invoked, and the memory is correctly freed. if B introduces any class members with their own destructors, they won't get invoked, but I'm only interested in the simple

Is this undefined behavior in C/C++

别等时光非礼了梦想. 提交于 2019-12-10 17:17:43
问题 int foo(int c){ return c; } int main(void){ int a=5,c; c = foo(--a) + a; } Will it invoke undefined behavior in C/C++? I think no it won't. After reading all the answers I can't figure out whether it is undefined behavior or unspecified behavior. 回答1: Yes it's undefined behavior - a and foo(--a) can be evaluated in any order. For further reference, see e.g. Sequence Point. There's a sequence point after the complete expression, and after evaluation of the argument to foo - but the order of

Is using any indeterminate value undefined or just those stored in objects with automatic storage?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:47:21
问题 According to C99 J.2, the behavior is undefined when: The value of an object with automatic storage duration is used while it is indeterminate What about all the other cases where an object has an indeterminate value? Do we also always invoke UB if we use them? Or do we invoke UB only when they contain a trap representation? Examples include: the value of an object allocated using malloc (7.20.3.3p2) [storing in non-automatic storage] a FILE* after calling fclose on it (7.19.3p4) [storing in

How can deleting a void pointer do anything other than invoke the global delete operator?

馋奶兔 提交于 2019-12-10 15:19:32
问题 The C++ standard very clearly and explicitly states that using delete or delete[] on a void -pointer is undefined behavior, as quoted in this answer: This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void . However, as I understand it, delete and delete[] do just two things: Call the appropriate destructor(s) Invoke the appropriate operator delete function, typically the global one There is a single-argument operator delete (as

const_cast and UB

試著忘記壹切 提交于 2019-12-10 14:32:41
问题 $5.2.11/7 - "[Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1). ]" The wordings of this section (C++03) are surprising to me. What is suprising are two things. a) Firstly, the use of 'may'. Why is it 'may'? Other places in the Standard are very definitive about the undefined behavior b) Why is that the casting away the

C Programming #define? [duplicate]

血红的双手。 提交于 2019-12-10 14:22:58
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) #include<stdio.h> #include<conio.h> #define SQ(x) x*x void main() { int a1 , a2; int b1 , b2; a1 = 2; a2 = 2; b1 = 0; b2 = 0; b1 = SQ(a1++); b2 = SQ(++a2); printf("Frist = %d",b1); printf("Second = %d",b2); } I know what is the output of the code. as #define work in other programe that way it is not working in above code Why.? 回答1:

Is it expected that a too large bitshift is undefined behavior in Rust?

心已入冬 提交于 2019-12-10 14:17:39
问题 When you run this code: #![allow(exceeding_bitshifts)] fn main() { const NUMBER: u64 = 0b_10101010; fn print_shift(i: u32) { println!("{:b}", NUMBER >> i); } print_shift(65); println!("{:b}", NUMBER >> 65); } You see that shifting the bits of a number with a value that exceed the bit length produces different behavior when doing at compile time or runtime. Is it a normal behavior? Is it documented somewhere? This is not in the list of documented undefined behavior. 回答1: No, this is not

Modifying constant object

∥☆過路亽.° 提交于 2019-12-10 13:17:35
问题 I was looking through the interview questions to a junior C++ developer position. The question is (quote): Is the following code correct ? struct Foo { int i; void foo ( void ) const { Foo* pointer = const_cast<Foo*>(this); pointer->i = 0; } }; I would answer: The code itself is valid according to the C++03 and c++11 standards and will compile successfully. But it may invoke an undefined behavior during assignment pointer->i = 0; if the instance of the class on which foo() is being called is

Is there any guarantee about whether code with UB should be reachable?

◇◆丶佛笑我妖孽 提交于 2019-12-10 13:07:51
问题 I have a code snippet from here: volatile int volatileInt; int usualInt; void function (unsigned x, unsigned y, unsigned z) { volatileInt = 0; usualInt = (x % y) / z; } int main() { function(rand(), rand(), rand()); } which I compile with Visual C++ 10 with /O2 and get this disassembly: 00403940 push ebx 00403941 push esi 276: function(rand(), rand(), rand()); 00403942 mov esi,dword ptr [__imp__rand (4050C0h)] 00403948 push edi 00403949 call esi 0040394B mov edi,eax 0040394D call esi 0040394F

When is pointer subtraction undefined in C?

血红的双手。 提交于 2019-12-10 12:39:31
问题 char *buf = malloc(bufsize) char *ptr = buf; … while(condition) { ptrdiff_t offset = ptr - buf; // <========== THIS LINE // offset will never be negative because we only ever *increase* ptr if ((size_t)offset > bufsize) { // we need more room bufsize += 128; buf = realloc(buf, bufsize); ptr = buf + offset; // buf might be in a completely new location } *ptr++ = … // write this byte } Is this valid or undefined ? I would have assumed that it's valid, but I read something about it being