new_and_delete.carbon 737 B

123456789101112131415161718192021222324252627282930313233343536
  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. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: Allocate A
  9. // CHECK:STDOUT: DESTRUCTOR A
  10. // CHECK:STDOUT: Delete A
  11. // CHECK:STDOUT: DESTRUCTOR A
  12. // CHECK:STDOUT: Return
  13. // CHECK:STDOUT: result: 0
  14. package ExplorerTest api;
  15. class A{
  16. fn Create() -> A {
  17. return {};
  18. }
  19. destructor[self: Self] {
  20. Print("DESTRUCTOR A");
  21. }
  22. }
  23. fn Main() -> i32 {
  24. Print("Allocate A");
  25. var pa: A* = heap.New(A.Create());
  26. Print("Delete A");
  27. heap.Delete(pa);
  28. Print("Return");
  29. return 0;
  30. }