Questions tagged [lifetime]

A variable's life-time is the time during which the variable is bound to a specific memory location. The life time starts when the variable is allocated and ends when it is deallocated.

Most of the time a lifetime is a synonym for the scope, for example in this code:

void foo()
{
    int x = 0; // lifetime of `x' begins here ──┐
    //                                          │
    printf("%d\n", x); //                       │
} // and finishes here ─────────────────────────┘

Rust:

The lifetime is a key concept in : it is a construct the compiler (also called the ) uses to ensure all borrows are valid.

Links:

2138 questions
16
votes
2 answers

C++ thread still `joinable()` after it finishes execution?

I have the following function: void threadProc(){ for (int i = 0; i < 5; ++i) { std::cout << "\n thread #" << std::this_thread::get_id() << " says hi"; } std::cout << "\n Finished executing thread #" <<…
Everyone
  • 1,751
  • 13
  • 36
16
votes
3 answers

Is the lifetime of a reference extended?

I'd like to pass a reference into a function. This code does not work, as I'd expect: struct A { }; void foo(A& a) { // do something with a } int main(int, char**) { foo(A()); } I get the compile error invalid initialization of non-const…
martinus
  • 17,736
  • 15
  • 72
  • 92
15
votes
1 answer

Is pointer arithmetic on allocated storage allowed since C++20?

In the C++20 standard, it is said that array types are implicit lifetime type. Does it mean that an array to a non implicit lifetime type can be implicitly created? The implicit creation of such an array would not cause creation of the array's…
Oliv
  • 17,610
  • 1
  • 29
  • 72
15
votes
1 answer

Why do the lifetimes on a trait object passed as an argument require Higher Ranked Trait Bounds but a struct doesn't?

How are lifetimes handled when there is a trait object passed to a function? struct Planet { i: T, } trait Spinner { fn spin(&self, value: T); } impl Spinner for Planet { fn spin(&self, value: T) {} } // foo2 fails: Due…
soupybionics
  • 4,200
  • 6
  • 31
  • 43
15
votes
5 answers

Why does Rust disallow mutable aliasing?

Rust disallows this kind of code because it is unsafe: fn main() { let mut i = 42; let ref_to_i_1 = unsafe { &mut *(&mut i as *mut i32) }; let ref_to_i_2 = unsafe { &mut *(&mut i as *mut i32) }; *ref_to_i_1 = 1; *ref_to_i_2 =…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
15
votes
3 answers

Late destruction of function parameters

According to 5.2.2/4 "Function call" in n4640 (8.2.2/4 in n4659) function parameters are created and destroyed in the context of the caller. And implementations are allowed to delay the destruction of function parameters to the end of the enclosing…
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
15
votes
5 answers

C++ constant reference lifetime (container adaptor)

I have code that looks like this: class T {}; class container { const T &first, T &second; container(const T&first, const T & second); }; class adapter : T {}; container(adapter(), adapter()); I thought lifetime of constant reference would be…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
15
votes
3 answers

Lifetime of the SSL session in https

We have an engaged (but friendly) discussion between coworkers about the life time of the SSL session underlying a https communication. When I establish a https connection to a server using a normal browser the underlying ssl creates a session…
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
15
votes
3 answers

What is the lifetime of a default argument temporary bound to a reference parameter?

I thought references only extend the lifetime of temporaries to the lifetime of the reference itself, but the output of the following snippet seems contradictory: #include struct X{ ~X(){ std::cout << "Goodbye, cruel world!\n"; } }; X…
Xeo
  • 129,499
  • 52
  • 291
  • 397
14
votes
2 answers

Specify Rust closures lifetime

I was making the executor/reactor while discovered this a lifetime problem. It is not related to async/Future and can be reproduced without async sugar. use std::future::Future; struct Runtime; fn start_with_runtime(closure: C) where C:…
orcy
  • 1,304
  • 14
  • 29
14
votes
1 answer

Is there a way to choose the lesser of two lifetimes?

I mean something like: fn minimum<'a, 'b>(x: &'a mut i32, y: &'b mut i32) -> &'min(a, b) mut i32 { (x < y) ? x : y } We don't know which reference will be chosen at lifetime, but the compiler knows in which scope both references are still valid…
aaalex88
  • 619
  • 4
  • 13
14
votes
3 answers

Why can I not return a mutable reference to an outer variable from a closure?

I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to…
soupybionics
  • 4,200
  • 6
  • 31
  • 43
14
votes
3 answers

Simulating activity death in Android

We know that when the system runs out of resources, an activity in background serializes its state and gets killed by the OS. When we resume it, the OS recovers the activity state by savedInstanceState passed to onCreate method. Considering we are…
Flávio Faria
  • 6,575
  • 3
  • 39
  • 59
14
votes
2 answers

Return reference with lifetime of self

I'd like to write some code like the following: struct Foo { foo: usize } impl Foo { pub fn get_foo<'a>(&'a self) -> &'self usize { &self.foo } } But this doesn't work, failing with invalid lifetime name: 'self is no longer a…
yong
  • 3,583
  • 16
  • 32
14
votes
1 answer

Expanding Rust Lifetime

I have a bit of code that I'm fighting with. It's a little helper function that should return a Vec<&str> to the calling function. I can't seem to get the lifetime right, though. Here is the code snippet: fn take_symbol<'a>(ch: &'a str, current: &'a…
wmaxlees
  • 605
  • 1
  • 5
  • 21