heap_alloc_lifetime.carbon 562 B

12345678910111213141516171819202122
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // CHECK:STDOUT: result: 0
  7. package ExplorerTest api;
  8. // The lifetime of a heap-allocated object is until it is explicitly
  9. // deleted. Its lifetime is unrelated to the procedure call stack.
  10. fn AllocateInteger() -> i32* {
  11. return heap.New(0);
  12. }
  13. fn Main() -> i32 {
  14. var p: i32* = AllocateInteger();
  15. var y: i32 = *p;
  16. heap.Delete(p);
  17. return y;
  18. }