object-lifetime Questions

4

Solved

According to this answer, IOptionsMonitor is registered in DI container as singleton and is capable of detecting changes through OnChange event subscription. It has a CurrentValue property. On the ...

3

Solved

Consider the following example. When bar is constructed, it gives it's base type (foo) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struc...

2

Solved

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 wrot...
Angellaangelle asked 15/7, 2014 at 13:49

2

Solved

Assuming X and Y are suitable types for such usage, is it UB to use std::start_lifetime_as<X> on an area of memory in one thread as one type and use std::start_lifetime_as<Y> on the exa...

2

For example i declare a global variable in service worker like var cacheVersion = "v1"; // line no 1 in sw.js what is the lifetime for this variable?. will this variable be there until s...
Moffett asked 5/12, 2020 at 12:42

2

Solved

Please consider this simplified c++14 program: #include <iostream> struct A { A() { std::cout << "A() "; } ~A() { std::cout << "~A() "; } }; int main() { ...
Patience asked 16/7, 2021 at 11:15

1

Solved

A friend of mine showed me a program in C++20: #include <iostream> struct A { A() {std::cout << "A()\n";} ~A() {std::cout << "~A()\n";} }; struct B { cons...

2

What does the standard have to say about function-local static initialization during program exit? EDIT: For clarity, I mean a case as in the code example - the local static b is constructed after ...
Lejeune asked 22/6, 2021 at 18:31

1

Solved

Another question cites the C++ standard: 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 — th...
Montpelier asked 8/4, 2021 at 11:21

1

Some types are defined by standard as implicit-lifetime types, and arrays are among them. Some functions implicitly create objects with implicit-lifetime (malloc etc are among them), with a list of...
Unboned asked 16/3, 2021 at 23:41

1

In the following short example, what can be said about the object the pointer f points to or used to point to just before returning from main? #include <vector> struct foo { std::vector<...
Commissar asked 7/9, 2020 at 1:43

2

Solved

NOTE: Bear with me, I feel a little "flame grilled" due to some discussions over here and here and some issues I reported here and here. Some background Ye olde (pre 10.4) FreeAndNil look...

4

Solved

Please let me begin with that I know it is a bad practice to call virtual functions from within a constructor/destructor. However, the behavior in doing so, although it might be confusing or not wh...
Pharyngoscope asked 9/6, 2020 at 17:13

8

Solved

I have a base class MyBase that contains a pure virtual function: void PrintStartMessage() = 0 I want each derived class to call it in their constructor then I put it in base class(MyBase) cons...

2

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 parti...
Honesty asked 20/10, 2017 at 0:43

7

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 c...
Edisonedit asked 24/10, 2017 at 11:15

7

Solved

Consider following sample code: class C { public: int* x; }; void f() { C* c = static_cast<C*>(malloc(sizeof(C))); c->x = nullptr; // <-- here } If I had to live with the uniniti...
Astra asked 5/6, 2016 at 17:49

2

Solved

This is a scenario you shouldn't ever do, but https://timsong-cpp.github.io/cppwp/class.cdtor#4 states: Member functions, including virtual functions ([class.virtual]), can be called during cons...

3

Solved

Is the value of this pointer guaranteed to be constant during a lifetime of a particular object? I can't imagine a case where it would change, but don't know whether I am not missing something.
Artificial asked 15/1, 2020 at 10:17

2

Solved

I know this is a pretty common subject, but as much as the typical UB is easy to find, I did not find this variant so far. So, I am trying to formally introduce Pixel objects while avoiding an act...

2

This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comments there.) This questio...
Psychosomatic asked 8/12, 2019 at 19:24

0

When another member of a union is accessed, the C++ standard used to be silent on what happens, but that was fixed to explain that member access to a union object was allowed for the purpose of ass...
Colliery asked 8/12, 2019 at 2:44

2

Solved

This is something that came up recently and which I feel shouldn't work as it apparently does: #include <iostream> #include <memory> int main() { std::shared_ptr<int>& ptr ...
Phosphatase asked 4/12, 2019 at 12:18

10

Solved

From http://en.cppreference.com/w/cpp/string/byte/memcpy: If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined. At my work, we have ...
Neotype asked 21/4, 2015 at 16:3

2

According to C++17 [basic.compound]/3: Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function)...
Urnfield asked 26/11, 2019 at 15:53

© 2022 - 2024 — McMap. All rights reserved.