I am reading n3290 draft of C++11 standard (as close as I could get to actual standard text), and I noticed that i = i++ + 1; produces undefined behavior. I have seen similar questions before, but they were answered in terms of older standards (Sequence points). New standard introduces instead concept of Sequencing before/after relation between expression and sub-expression executions. 1.9 1
我正在阅读C ++ 11标准的n3290草案(尽可能接近实际的标准文本),并且我注意到i = i++ + 1; 产生未定义的行为。 我以前见过类似的问题,但是他们是根据旧标准(序列点)来回答的。 新标准在表达式和子表达式执行之前/之后引入了Sequencing的概念。 1.9 13之前排序的是由单个线程(1.10)执行的评估之间的不对称,传递,成对关系,这会导致这些评估之间的偏序。 给定任何两个评估A和B,如果A在B之前被排序,那么A的执行应
I have an array of unsigned integers that need to store pointers to data and functions as well as some data. In the device I am working with, the sizeof pointer is the same as sizeof unsigned int. How can I cast pointer to function into unsigned int? I know that this makes the code not portable, but it is micro controller specific. I tried this: stackPtr[4] = reinterpret_cast<unsigned int
我有一组无符号整数,需要存储指向数据和函数的指针以及一些数据。 在我使用的设备中,sizeof指针与sizeof unsigned int相同。 我如何将指针转换为无符号整型函数? 我知道这使得代码不可移植,但它是微控制器特定的。 我试过这个: stackPtr[4] = reinterpret_cast<unsigned int>(task_ptr); 但它给我一个错误“无效类型转换” 将其转换为void指针,然后转换为int是很麻烦的。 stackPtr[4] = reinterpret_cast<u
What are all the common undefined behaviours that a C++ programmer should know about? Say, like: a[i] = i++; Pointer Dereferencing a NULL pointer Dereferencing a pointer returned by a "new" allocation of size zero Using pointers to objects whose lifetime has ended (for instance, stack allocated objects or deleted objects) Dereferencing a pointer that has not yet been definit
C ++程序员应该知道的所有常见的未定义行为是什么? 比如说: a[i] = i++; 指针 解引用NULL指针 取消引用大小为零的“新”分配返回的指针 使用指向其生命周期已结束的对象的指针(例如,堆栈分配的对象或已删除的对象) 解引用尚未明确初始化的指针 执行指针算术,该算术可在数组的边界之外(上方或下方)生成结果。 将指针解引用超出数组末尾的位置。 将指针转换为不兼容类型的对象 使用memcpy复制重叠的缓冲
Consider the classical sequence point example: i = i++; The C and C++ standards state that the behavior of the above expression is undefined because the = operator is not associated with a sequence point. What confuses me is that ++ has a higher precedence than = and so, the above expression, based on precedence, must evaluate i++ first and then do the assignment. Thus, if we start with i =
考虑经典的顺序点示例: i = i++; C和C ++标准声明上述表达式的行为是未定义的,因为=操作符不与序列点关联。 令我困惑的是, ++的优先级高于= ,因此,基于优先级的上述表达式必须先评估i++ ,然后再进行分配。 因此,如果我们从i = 0开始,我们应该总是以i = 0 (或者i = 1 ,如果表达式为i = ++i )结束而不是未定义的行为。 我错过了什么? 运算符优先级(和关联性)说明表达式被解析和执行的顺序。 然而,这并没有
This question came up while I was reading (the answers to) So why is i = ++i + 1 well-defined in C++11? I gather that the subtle explanation is that (1) the expression ++i returns an lvalue but + takes prvalues as operands, so a conversion from lvalue to prvalue must be performed; this involves obtaining the current value of that lvalue (rather than one more than the old value of i ) and must
这个问题出现在我阅读(答案)的时候,那么为什么我在C ++ 11中明确地定义了i = ++ i + 1? 我收集到微妙的解释是(1)表达式++i返回一个左值,但+将prvalues作为操作数,所以必须执行从左值到左值的转换; 这涉及获得与左值(比的旧值,而不是一个多的当前值i ),并从增量副作用(即,在更新之后,因此必须测序i )(2)分配的LHS也是因此其价值评估不涉及获取i的当前价值; 虽然这个值计算与RHS的值计算没有序列关系,但这
Possible Duplicate: What is The Rule of Three? The following code outputs garbage at best or crashes: #include <stdio.h> #include <stdlib.h> #include <string.h> class C { public: char* s; C(char* s_) { s=(char *)calloc(strlen(s_)+1,1); strcpy(s,s_); }; ~C() { free(s); }; }; void func(C c) {}; void main() { C o="hello";
可能重复: 什么是三项规则? 以下代码最好输出垃圾或崩溃: #include <stdio.h> #include <stdlib.h> #include <string.h> class C { public: char* s; C(char* s_) { s=(char *)calloc(strlen(s_)+1,1); strcpy(s,s_); }; ~C() { free(s); }; }; void func(C c) {}; void main() { C o="hello"; printf("hello: %sn",o.s); // works ok fu
Possible Duplicate: What is The Rule of Three? Why is it recommended to provide implementation of copy constructor instead of using compiler provided "default copy constructor" ? 如果你的类包含指针成员, 它们是动态分配的,那么你需要提供你自己的拷贝构造版本,因为默认版本只是对它们进行浅度拷贝。 It's not. The default copy constructor is perfect in 99.9% of cases. The excepti
可能重复: 什么是三项规则? 为什么建议提供复制构造函数的实现而不是使用编译器提供的“默认复制构造函数”? 如果你的类包含指针成员, 它们是动态分配的,那么你需要提供你自己的拷贝构造版本,因为默认版本只是对它们进行浅度拷贝。 不是。 在99.9%的情况下,默认的拷贝构造函数是完美的。 拥有指针的类的例外。 这里默认拷贝构造函数的浅拷贝不能像初学者所期望的那样工作。 但是,你不应该在你的课堂上有指针
This question already has an answer here: What is The Rule of Three? 8 answers The Grid class doesn't have a copy-assignment operator, so the compilers default generated version will be used instead. It's very simple, and does only a shallow copy of the members. That means that the pointers created for the temporary object Grid(4, 4) are copied (just the pointers, and not what they
这个问题在这里已经有了答案: 什么是三项规则? 8个答案 Grid类没有复制赋值操作符,因此将使用编译器默认生成的版本。 这很简单,只做一个会员的简单副本。 这意味着为临时对象Grid(4, 4)创建的指针被复制(只是指针,而不是它们指向的内容),并且临时对象被销毁时,指针(在临时对象的析构函数中)。 这给你留下一个对象g ,指向现在被删除的内存。 我建议你阅读三条规则。
This question already has an answer here: What is The Rule of Three? 8 answers The types of the member variables is not important for that(1), their semantics are. The rule is simple: If you don't provide a copy constructor, the compiler will try to generate one for you. This default-generated one will perform the default copy operation on all member variables. For class types, this
这个问题在这里已经有了答案: 什么是三项规则? 8个答案 (1)成员变量的类型并不重要,它们的语义是。 规则很简单: 如果你没有提供拷贝构造函数,编译器会尝试为你生成一个拷贝构造函数。 这个默认生成的将对所有成员变量执行默认的复制操作。 对于类类型,这意味着调用复制构造函数。 对于原始类型,这意味着按位复制。 如果默认生成的构造函数执行你所需要的,不要声明你自己的。 如果它不能满足你的需求,请
This question already has an answer here: Double free or corruption…but why? 6 answers What is The Rule of Three? 8 answers Your type manages resources (a dynamically allocated array) but does not implement the rule of three. When you do this: q.push_back(t); q makes a copy of t , which it owns. So now you have two copies of an object referring to the same data, and attempting to call
这个问题在这里已经有了答案: 双免费或腐败......但为什么? 6个答案 什么是三项规则? 8个答案 你的类型管理资源(一个动态分配的数组),但没有实现三的规则。 当你这样做时: q.push_back(t); q制作一份它拥有的t的副本。 所以现在你有两个引用相同数据的对象副本,并尝试在其中调用delete 。 您需要实现复制构造函数和赋值运算符。 或者使用管理自己资源的类,例如std::string或std::vector 。 在已经删除