heap_alloc_lifetime.carbon 563 B

1234567891011121314151617181920212223
  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. package ExplorerTest api;
  7. // The lifetime of a heap-allocated object is until it is explicitly
  8. // deleted. Its lifetime is unrelated to the procedure call stack.
  9. fn AllocateInteger() -> i32* {
  10. return heap.New(0);
  11. }
  12. fn Main() -> i32 {
  13. var p: i32* = AllocateInteger();
  14. var y: i32 = *p;
  15. heap.Delete(p);
  16. return y;
  17. }
  18. // CHECK:STDOUT: result: 0