object-lifetime

What are the changes, if any, to the memcpy lifetime initalization rules in the new standard?

好久不见. 提交于 2019-12-01 06:53:47
问题 As far as I am aware, memcpy into uninitialized storage cannot safely be used to create an copy of the source object. However, in this thread from last year on the open-std WG21 "ub" list, a participant refers to the new memcpy lifetime-initiation rules : This doesn’t seem to rise to the level of a bug report, but it might be relevant to the new memcpy lifetime-initiation rules. Will they ascribe some static type to the source and destination bytes? Based on the context of the question and

.NET - Finalizers and exit(0)

廉价感情. 提交于 2019-12-01 06:13:48
问题 I have a .NET C# / C++ app which uses a call to exit(0) (from <stdlib.h> ) in a thread in order to terminate. The strange part is, under some circumstances, the finalizers of the managed objects are called right after the call to exit , and in other circumstances, they are not called at all. The circumstances are pretty deterministic - the app calls some methods from an external plugin dll (written in unmanaged C) during its lifetime. If I use dll A, the finalizers are always called. If I use

Does destroying and recreating an object make all pointers to this object invalid?

旧时模样 提交于 2019-12-01 03:49:14
This is a follow-up to this question . Suppose I have this code: class Class { public virtual method() { this->~Class(); new( this ) Class(); } }; Class* object = new Class(); object->method(); delete object; which is a simplified version of what this answer suggests. Now once a destructor is invoked from within method() the object lifetime ends and the pointer variable object in the calling code becomes invalid. Then the new object gets created at the same location. Does this make the pointer to the object in the calling valid again? This is explicitly approved in 3.8:7: 3.8 Object lifetime

Is it wrong to use braces for variable scope purposes?

◇◆丶佛笑我妖孽 提交于 2019-12-01 02:18:27
I sometimes use braces to isolate a block of code to avoid using by mistake a variable later. For example, when I put several SqlCommand s in the same method, I frequently copy-paste blocks of code, ending by mixing the names and executing twice some commands. Adding braces helps to avoid this situation, because using a wrong SqlCommand in a wrong place will result in an error. Here's an illustration: Collection<string> existingCategories = new Collection<string>(); // Here a beginning of a block { SqlCommand getCategories = new SqlCommand("select Title from Movie.Category where SourceId =

Lifetime extension and the conditional operator

隐身守侯 提交于 2019-11-30 13:42:33
问题 local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression, but uses the conditional operator? std::string&& c = condition ? std::string("hello") : std::string("world"); What if one of the results is a temporary object, but the other one isn't? std::string d = "hello"; const std::string& e = condition ? d :

Existence of objects created in C functions

本秂侑毒 提交于 2019-11-30 11:09:43
It has been established (see below) placement new is required to create objects int* p = (int*)malloc(sizeof(int)); *p = 42; // illegal, there isn't an int Yet that is a pretty standard way of creating objects in C. The question is, does the int exist if it is created in C, and returned to C++? In other words, is the following guaranteed to be legal? Assume int is the same for C and C++. foo.h #ifdef __cplusplus extern "C" { #endif int* foo(void); #ifdef __cplusplus } #endif foo.c #include "foo.h" #include <stdlib.h> int* foo(void) { return malloc(sizeof(int)); } main.cpp #include "foo.h"

Lifetime extension and the conditional operator

情到浓时终转凉″ 提交于 2019-11-30 08:09:14
local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression, but uses the conditional operator? std::string&& c = condition ? std::string("hello") : std::string("world"); What if one of the results is a temporary object, but the other one isn't? std::string d = "hello"; const std::string& e = condition ? d : std::string("world"); Does C++ mandate the lifetime of the temporary be extended when the condition is

Destruction of return value on destructor exception

岁酱吖の 提交于 2019-11-30 07:50:54
I have the following code: #include <stdexcept> #include <iostream> struct ok { int _n; ok(int n) : _n(n) { std::cerr << "OK" << n << " born" << std::endl; } ~ok() { std::cerr << "OK" << _n << " gone" << std::endl; } }; struct problematic { ~problematic() noexcept(false) { throw std::logic_error("d-tor exception"); } }; ok boo() { ok ok1{1}; problematic p; ok ok2{2}; return ok{3}; // Only constructor is called... } int main(int argc, char **argv) { try {boo();} catch(...) {} } I see that he destructor of ok{3} is not called, the output is: OK1 born OK2 born OK3 born OK2 gone OK1 gone Is it the

memcpy/memmove to a union member, does this set the 'active' member?

折月煮酒 提交于 2019-11-30 01:40:04
Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy , it copies from the address of a plain old uint32_t , which is not contained within a union. Also, I am copying (via memcpy ) to a specific member of a union ( u.a16 or &u.x_in_a_union , not directly to the entire union itself ( &u ) C++ is quite strict about unions - you should read from a member only if that was the last member that was written to: 9.5 Unions [class.union] [[c++11]] In a union, at most one of the non-static data members can be active at any time, that is, the

Is there a way to disable binding a temporary to a const reference?

时光毁灭记忆、已成空白 提交于 2019-11-29 17:01:33
In C++ it is possible to bind a temporary to a const reference: struct A {}; int main() { const A& a = A(); } Is there any way to disable this for some particular class A so that it would be impossible to bind a temporary of this class to a const reference? No, and if you need to do this, you're doing something else wrong. In general there doesn't seem to be a way to do disable binding a temporary to a const reference. However, to give a substantiated answer, I'd like to cite the C++ 2003 standard: If the initializer expression is an rvalue, with T2 a class type, and “cv1 T1” is reference