/*
 * Copyright ©2026 Soham Pardeshi. All rights reserved.
 * Permission is hereby granted to students registered for University of
 * Washington CSE 333 for use solely during Summer Quarter 2026 for
 * purposes of the course. No other use, copying, distribution, or
 * modification is permitted without prior written consent. Copyrights
 * for third-party components of this work must be honored. Instructors
 * interested in reusing these course materials should contact the author.
 */

#include <cstdlib>
#include <memory>

using std::shared_ptr;

// shared_ptr reference counts, but a reference count cannot free a CYCLE.
// Here head and head->next point at each other, so neither count ever
// reaches 0 and both nodes leak.
//
//   make sharedcycle && valgrind --leak-check=full ./sharedcycle
//   -> "definitely lost" / "still reachable" bytes
//
// See weakcycle.cc for the fix.
struct A {
  shared_ptr<A> next;
  shared_ptr<A> prev;
};

int main(int argc, char** argv) {
  shared_ptr<A> head(new A());
  head->next = shared_ptr<A>(new A());
  head->next->prev = head;  // cycle: refcounts are now 2 and 1, never 0

  return EXIT_SUCCESS;
}