object-lifetime

Can the storage of trivially copyable objects be safely reallocated with realloc?

喜夏-厌秋 提交于 2020-01-14 07:48:08
问题 I know that trivially copyable objects can safely be copied my malloc into an appropriate storage location 1 and that the destination object will have the same value as the source. Is this also possible with realloc ? That is, if realloc some storage containing some objects of type T , and realloc decides to move and copy the block, will the objects in the newly allocated storage be intact and have started their lifetime, and will the lifetime of the objects in the old storage be safely ended

Boost shared_from_this and destructor

眉间皱痕 提交于 2020-01-12 10:08:46
问题 I found that it is not allowed to call shared_from_this in the destructor from a class: https://svn.boost.org/trac/boost/ticket/147 This behavior is by design. Since the destructor will destroy the object, it is not safe to create a shared_ptr to it as it will become dangling once the destructor ends. I understand the argument, but what if I need a "shared_from_this" pointer for cleaning up references ( not for sharing owner ship). Here is an example where I'm not using shared_ptr: class A{

Lifetime of object which has vacuous initialization

筅森魡賤 提交于 2020-01-11 06:58:17
问题 Current draft standard says (previous standards have similar wording) in [basic.life/1]: The lifetime of an object or reference is a runtime property of the object or reference. An object is said to have non-vacuous initialization if it is of a class or aggregate type and it or one of its subobjects is initialized by a constructor other than a trivial default constructor. [ Note: Initialization by a trivial copy/move constructor is non-vacuous initialization. — end note ] The lifetime of an

Lifetime of object is over before destructor is called?

余生颓废 提交于 2020-01-09 05:07:45
问题 I don't understand this: 3.8/1 "The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts , or — the storage which the object occupies is reused or released." If the lifetime ends before the destructor starts, doesn't that mean accessing members in the destructor is undefined behavior? I saw this quote too: 12.7 "For an object with a non-trivial destructor, referring to any non-static member or base class of the

How long does a string constant live in c++?

柔情痞子 提交于 2020-01-03 14:16:21
问题 I've been wondering, how long does a string constant live in C++. For example, if I create some const char *str = "something" inside a function, would it be safe to return the value of str? I wrote a sample program and was really surprised to see that such returned value still stored that string. Here is the code: #include <iostream> using namespace std; const char *func1() { const char *c = "I am a string too"; return c; } void func2(const char *c = "I'm a default string") { cout << c <<

What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?

こ雲淡風輕ζ 提交于 2020-01-01 05:11:11
问题 As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void* -to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer, and so on, but it never seems to explicitly say that casting an arbitrary void pointer yields a pointer to the

What is the order of destruction of objects in VBScript?

两盒软妹~` 提交于 2019-12-30 05:00:06
问题 In what order are objects in a .vbs destroyed? That is, given these globals: Set x = New Xxx Set y = New Yyy I'm interested in answers to any of the following. For instances of classes implemented in the .VBS, in what order will Class_Terminate be called? Cursory poking suggests in the order ( not reverse order!) of creation, but is this guaranteed? EDIT : I understand that Class_Terminate will be called when the last last reference to an object is released. What I meant was: in what order

Lifetime of lambda objects in relation to function pointer conversion

五迷三道 提交于 2019-12-28 12:47:36
问题 Following this answer I'm now wondering what the rules are for the lifetime of lambdas and how the relate to the lifetime of function pointers which are created by automatic conversion. There are several questions about the lifetime of lambdas (e.g. here and here), in which case the answers are "they behave exactly like you wrote the full functor object yourself", however neither address the conversion to function pointer which could quite sensibly be a special case. I put together this small

Lifetime of lambda objects in relation to function pointer conversion

风格不统一 提交于 2019-12-28 12:46:26
问题 Following this answer I'm now wondering what the rules are for the lifetime of lambdas and how the relate to the lifetime of function pointers which are created by automatic conversion. There are several questions about the lifetime of lambdas (e.g. here and here), in which case the answers are "they behave exactly like you wrote the full functor object yourself", however neither address the conversion to function pointer which could quite sensibly be a special case. I put together this small

Implementing a search() method to handle optional case sensitivity [duplicate]

二次信任 提交于 2019-12-24 21:42:06
问题 This question already has answers here : Return local String as a slice (&str) (3 answers) How do I pass modified string parameters? (2 answers) Closed last year . In The Rust Programming Language , there is a chapter which implements a minigrep. Instead of implementing a second search_case_insensitive() method, I wanted to implement a single API which would duplicate code. That attempt went something like this: pub fn search<'a>(query: &str, lines: &'a str, case_insensitive: bool) -> Vec<&'a