undefined-behavior

Default value to non initialized variables

不打扰是莪最后的温柔 提交于 2019-12-29 09:16:08
问题 I'm reading this tutorial about debugging. I pasted the factorial code in my .c archive: #include <stdio.h> int main() { int i, num, j; printf ("Enter the number: "); scanf ("%d", &num ); for (i=1; i<num; i++) j=j*i; printf("The factorial of %d is %d\n",num,j); } When I run the executable, it always print 0 , however, the author of the tutorial says that it return numbers garbage value. I've googled about this and I've read that this is right, except for static variables. So it should return

string s; &s+1; Legal? UB?

只愿长相守 提交于 2019-12-29 08:46:50
问题 Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { string myAry[] = { "Mary", "had", "a", "Little", "Lamb" }; const size_t numStrs = sizeof(myStr)/sizeof(myAry[0]); vector<string> myVec(&myAry[0], &myAry[numStrs]); copy( myVec.begin(), myVec.end(), ostream_iterator<string>(cout, " ")); return 0; } Of interest here is &myAry[numStrs] : numStrs is equal to 5, so &myAry[numStrs] points to

Implementing a std::vector like container without undefined behavior

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 08:07:55
问题 It may surprise some coders and, as surprising as it can be, it is not possible to implement std::vector without non-standard support of the compilers. The problem essentially resides on the ability to perform pointer arithmetic on a raw storage region. The paper, p0593: Implicit creation of objects for low-level object manipulation, that appears in @ShafikYaghmour answer, exposes clearly the problematic and proposes modification of the standard in order to make implementation of vector like

Why is calling non virtual member function on deleted pointer an undefined behavior?

那年仲夏 提交于 2019-12-29 01:43:25
问题 As, the title says: Why is calling non virtual member function on deleted pointer an undefined behavior? Note the Question does not ask if it is an Undefined Behavior, it asks Why it is undefined behavior. Consider the following program : #include<iostream> class Myclass { //int i public: void doSomething() { std::cout<<"Inside doSomething"; //i = 10; } }; int main() { Myclass *ptr = new Myclass; delete ptr; ptr->doSomething(); return 0; } In the above code, the compiler does not actually

Why is it that we can write outside of bounds in C?

你。 提交于 2019-12-28 06:59:24
问题 I recently finished reading about virtual memory and I have a question about how malloc works within the Virtual address space and Physical Memory. For example (code copied from another SO post) void main(){ int *p; p=malloc(sizeof(int)); p[500]=999999; printf("p[0]=%d\n",p[500]); //works just fine. } Why is this allowed to happen? Or like why is that address at p[500] even writable? Here is my guess. When malloc is called, perhaps the OS decides to give the process an entire page. I will

Is it undefined behavior to dereference a dangling pointer?

一个人想着一个人 提交于 2019-12-28 06:46:09
问题 I can't find where in the standard that it says this program is undefined: #include <iostream> int main() { int *p; { int n = 45; p = &n; } std::cout << *p; } None of the cases in §3.8 object lifetime seem to apply here. 回答1: I'm not 100% sure because of the wording but it looks like this is covered by 3.8/6 (the reason I think this interpretation is correct is because of the non-normative example in 3.8/5, // undefined behavior, lifetime of *pb has ended ): ...after the lifetime of an object

Behaviour and Order of evaluation in C# [duplicate]

元气小坏坏 提交于 2019-12-28 06:36:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C#: Order of function evaluation (vs C) Code snippet: i += ++i; a[++i] = i; int result = fun() - gun(); //statement of similar kind Are their behavior well-defined in C#? In C++, such code invokes undefined/unspecified behavior. Please also quote the relevant sections from the language specification in your response! 回答1: The key here is the table in "1.4 Expressions", and "7.3.1 Operator precedence and

behavior of const_cast in C++ [duplicate]

会有一股神秘感。 提交于 2019-12-28 04:31:08
问题 This question already has answers here : Two different values at the same memory address (7 answers) Closed 2 years ago . Here is my problem, the problem is in comments const int a = 5; const_cast<int&>(a)=7; //throw over const attribute in a,and assign to 7 std::cout<<a<<std::endl; //why still out put 5!!!!!!!!!! Who can tell me why, and some books account these problems to recommend ? Thanks! 回答1: As-written the way you're doing this is undefined behavior. If you wanted to see the effects

C code with undefined results, compiler generates invalid code (with -O3)

走远了吗. 提交于 2019-12-28 04:15:11
问题 I know that when you do certain things in a C program, the results are undefined. However, the compiler should not be generating invalid (machine) code, right? It would be reasonable if the code did the wrong thing, or if the code generated a segfault or something... Is this supposed to happen according to the compiler spec, or is it a bug in the compiler? Here's the (simple) program I'm using: int main() { char *ptr = 0; *(ptr) = 0; } I'm compiling with -O3 . That shouldn't generate invalid

The behaviour of floating point division by zero

可紊 提交于 2019-12-28 03:52:04
问题 Consider #include <iostream> int main() { double a = 1.0 / 0; double b = -1.0 / 0; double c = 0.0 / 0; std::cout << a << b << c; // to stop compilers from optimising out the code. } I have always thought that a will be +Inf, b will be -Inf, and c will be NaN. But I also hear rumours that strictly speaking the behaviour of floating point division by zero is undefined and therefore the above code cannot considered to be portable C++. (That theoretically obliterates the integrity of my million