undefined-behavior

Pointer to deallocated location Is it a Undefined Behavior?

女生的网名这么多〃 提交于 2019-12-13 10:14:36
问题 Pointer to deallocated location Is it a Undefined Behavior? http://ideone.com/Qp3uY int *p = new int; *p = 10; delete p; *p = 10; cout << *p << endl; 回答1: There mere existence of a pointer to a deallocated location is not undefined behavior in itself. Attempting to dereference that pointer does produce undefined behavior though. 回答2: Dereferencing a deleted pointer is an undefined operation. Don't do it. 回答3: This is undefined behavior: If the argument given to a deallocation function in the

When is it okay to do/use something that has unspecified behaviour? [duplicate]

丶灬走出姿态 提交于 2019-12-13 09:20:45
问题 This question already has answers here : Undefined, unspecified and implementation-defined behavior (8 answers) Closed 5 years ago . In C++, there are things that come up that are somewhere between well-defined and undefined. Specifically, those are called implementation defined and unspecified . Right now, I'm interested in the unspecified stuff. When is it okay to use such features, and when should they be avoided? Are there good examples of unspecified behaviour being a part of correct

Different versions of gcc differently compile the same code [duplicate]

两盒软妹~` 提交于 2019-12-13 08:20:00
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 4 months ago . I have switched from MS Visual Studio to gcc, and currently I am trying to recompile some of the codes I have written in VS by gcc. Now I come across something odd. Simply explained, consider the following code, but first, note that I already know it's a very bad code (which is not the point here) #include <iostream> int main() { int i = 0, a[10

Is accessing an array element using a char undefined behaviour?

◇◆丶佛笑我妖孽 提交于 2019-12-13 01:59:48
问题 Since it's not clear what's undefined behaviour and what's not in C, I'm wondering if accessing an array element using a char is or not undefined behaviour. For example: char c = 'A'; int a[3000]; printf("%i\n", a[c]); I know that actually chars and ints are somehow interchangeable, but still, I'm not sure. 回答1: Syntactically, a[c] is a valid expression as long as c is an integer type or can be promoted to an integer type. From the C99 Standard: 6.5.2.1 Array subscripting 1 One of the

Program crashes when `if (variable % 2 == 0)`

一笑奈何 提交于 2019-12-13 01:35:02
问题 I'm writing a program that finds perfect numbers . Having read about these perfect numbers I came across a list of them: List of perfect numbers. At the moment the output is: 28 // perfect 496 // perfect 8128 // perfect 130816 // not perfect 2096128 // not perfect 33550336 // perfect I decided to create array and put it with numbers, which divide the number wholly (without the rest). So I will be able to verify if it is a perfect number or not by adding all elements of the array. But app

Is it undefined behavior to reinterpret_cast an object of an unrelated type to an empty class

对着背影说爱祢 提交于 2019-12-13 01:07:35
问题 It it undefined behavior to cast an unrelated type to an empty base class? And then use that address to construct a derived type that inherits from that empty base? For example class Derived : public EmptyBase { public: template <typename T> Derived(T&& t) : EmptyBase{std::forward<T>(t)} {} using EmptyBase::print; }; and then is it undefined to do something like this static auto shim = 1; auto derived = Derived{*reinterpret_cast<EmptyBase*>(&shim)}; derived.print(); The standard guarantees

A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

佐手、 提交于 2019-12-13 00:39:18
问题 In C++, can the value of a const & change? Well, of course it cannot change, can it? That's what const means. Moreover, listen to Stroustrup: A const lvalue reference refers to a constant, which is immutable from the point of view of the user of the reference. But what about this? #include <iostream> int main() { int a = 0; const int& r = a; const int old_r = r; ++a; const int new_r = r; std::cout << "old_r == " << old_r << " but new_r == " << new_r << std::endl; return 0; } On my machine,

Does casting to an unrelated reference type violate the strict aliasing rule?

岁酱吖の 提交于 2019-12-12 21:33:02
问题 The strict aliasing rule says 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 dynamic type of the object, — a type similar (as defined in 4.4) to the dynamic type of the object, — a type that is the signed or unsigned type corresponding to the dynamic type of the object, — a type that is the signed or unsigned type corresponding

Is this hack a defined behavior for T4

落花浮王杯 提交于 2019-12-12 17:16:31
问题 I recently set out on an expedition to unit-test a rather complex T4 class. I've arrived at a major breakthough, but I'm afraid the observed behavior may only be coincidental(ie, may break in future versions of Visual Studio) I basically have something like this: MainTemplate.tt: <#@ include file="generator.tt.cs" #> And then in generator.tt.cs I have //<#+ class code { .... } //#> The observed behavior of this is that I can both use the declared classes and such from the T4 template AND

Undefined behavior and sequence points

拈花ヽ惹草 提交于 2019-12-12 17:09:39
问题 What are "sequence points"? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i; , to make myself feel better. Why should I stop using them? If you've read this, be sure to visit the follow-up question Undefined behavior and sequence points reloaded . (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that