Questions tagged [object-lifetime]

The object lifetime (or life cycle) of an object in object-oriented programming is the time between an object is created (also known as instantiation or construction) till the object is no longer used and then destructed or freed.

In typical case, the process is as follows:

  1. Memory allocation and binding: Calculate the size of the object (usually the alligned size of each member of the object), allocating memory space with the size of an object plus the growth later, if possible to know in advance and binding methods.
  2. Constructor call and execution: First the constructor of superclass(es) are called, then the constructor of the object.
  3. Object use.
  4. Destructor call and execution: First the destructor of the object are called, then the destructor(s) of the superclass(es).
  5. Memory deallocation and unbinding.
402 questions
26
votes
3 answers

Why does calling std::string.c_str() on a function that returns a string not work?

I have the following code: std::string getString() { std::string str("hello"); return str; } int main() { const char* cStr = getString().c_str(); std::cout << cStr << std::endl; // this prints garbage } What I thought would happen…
hacksoi
  • 1,301
  • 13
  • 23
24
votes
3 answers

Is it legal to call member functions after an object has been explicitly destroyed but before its memory was deallocated?

I have this code: struct data { void doNothing() {} }; int main() { data* ptr = new data(); ptr->~data(); ptr->doNothing(); ::operator delete(ptr); } Note that doNothing() is being called after the object has been destroyed but…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
23
votes
1 answer

Struct that owns some data and a reference to the data

Construction of an object allocates data needed for lifetime of that object, but also creates another object that needs to keep references to the data: pub fn new() -> Obj { let data = compute(); Obj { original: data, …
Kornel
  • 97,764
  • 37
  • 219
  • 309
23
votes
2 answers

Spurious warning about binding temporary to reference member in constructor

I understand that if a temporary is bound to a reference member in the constructor's initializer list, the object will be destroyed as the constructor returns. However, consider the following code: #include #include using…
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
21
votes
4 answers

C# Thread object lifetime

Suppose I have a code as follows: int Main() { if (true) { new Thread(()=> { doSomeLengthyOperation(); }).Start(); } while (true) { //do nothing } } There are 2 threads, I'm…
jonathan_ou
  • 756
  • 1
  • 7
  • 17
20
votes
6 answers

Best practice to do nested TRY / FINALLY statement

Hi What is the best way to do nested try & finally statements in delphi? var cds1 : TClientDataSet; cds2 : TClientDataSet; cds3 : TClientDataSet; cds4 : TClientDataSet; begin cds1 := TClientDataSet.Create(application ); try …
Charles Faiga
  • 11,665
  • 25
  • 102
  • 139
20
votes
2 answers

Lifetime extension and the conditional operator

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,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
19
votes
3 answers

Is the value of `this` pointer constant during the object's lifetime?

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.
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
19
votes
7 answers

Existence of objects created in C functions

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…
Passer By
  • 19,325
  • 6
  • 49
  • 96
19
votes
5 answers

Is it wrong to use braces for variable scope purposes?

I sometimes use braces to isolate a block of code to avoid using by mistake a variable later. For example, when I put several SqlCommands in the same method, I frequently copy-paste blocks of code, ending by mixing the names and executing twice some…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
19
votes
7 answers

Invoking virtual method in constructor: difference between Java and C++

In Java: class Base { public Base() { System.out.println("Base::Base()"); virt(); } void virt() { System.out.println("Base::virt()"); } } class Derived extends Base { public Derived() { System.out.println("Derived::Derived()");…
Xiao Jia
  • 4,169
  • 2
  • 29
  • 47
17
votes
7 answers

Is circumventing a class' constructor legal or does it result in undefined behaviour?

Consider following sample code: class C { public: int* x; }; void f() { C* c = static_cast(malloc(sizeof(C))); c->x = nullptr; // <-- here } If I had to live with the uninitialized memory for any reason (of course, if possible, I'd…
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
17
votes
1 answer

What is the order of destruction of function parameters?

This is a follow-up to my previous question What is the order of destruction of function arguments? because I accidentally confused arguments with parameters. Thanks to Columbo and T.C. for clearing me of terminology confusion in the comments of…
jotik
  • 17,044
  • 13
  • 58
  • 123
16
votes
2 answers

Function-local static initialization during program exit

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 another static a is constructed(so supposedly b…
theroyn
  • 506
  • 5
  • 16
16
votes
2 answers

Does malloc return an "invalid pointer value" in C++17?

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), or a pointer past the end of an object (8.7), or the null…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
1
2
3
26 27