int main () { int i, j, nrot; float a[N][N], d[N], v[N][N]; for (i = 0; i <= N; i++) for (j = 0; j < N; j++) a[i][j] = 1.2 * random_number_gen (); for (i = 0; i <= N; i++) for (j = 0; j < N; j++) v[i][j] = 0.7 * random_number_gen (); for (i = 0; i <= N; i++) d[i] = random_number_gen (); jacob (a, d, v, &nrot); printf( "\nnumber of Jacobi applied: %i\n", nrot); return 1; }
int main() { std::cout << "start of main" << std::endl; // ==================================================== // SINGLE OWNER SMART POINTERS // ==================================================== // first, without smart pointers! Balloon* alice(new Balloon("Hello Kitty")); // now, with our homemade single owner smart pointer dsAutoPtr<Balloon> bob(new Balloon("Spiderman")); // both alice & bob work like regular pointers... alice->print(); bob->print(); // // CHECKPOINT 2A: INSERT NECESSARY EXPLICIT DEALLOCATION // delete alice; // ==================================================== // SIMPLE SHARED POINTERS // ==================================================== // first, without smart pointers Balloon* cathy(new Balloon("Buzz Lightyear")); Balloon* daniel(cathy); Balloon* elaine(new Balloon("Pokemon")); Balloon* fred(elaine); daniel = fred; fred = NULL; elaine = cathy; cathy = NULL; // // CHECKPOINT 2B: INSERT NECESSARY EXPLICIT DEALLOCATION // delete elaine; delete daniel; daniel = NULL; elaine = NULL; // now, with our homemade shared pointer dsSharedPtr<Balloon> cathy2(new Balloon("Buzz Lightyear2")); dsSharedPtr<Balloon> daniel2(cathy2); dsSharedPtr<Balloon> elaine2(new Balloon("Pokemon2")); dsSharedPtr<Balloon> fred2(elaine2); daniel2 = fred2; fred2 = NULL; elaine2 = cathy2; cathy2 = NULL; // NOTE: no explicit destruction required! daniel2 = NULL; elaine2 = NULL; // ==================================================== // SHARED POINTERS WITH INTERCONNECTED STRUCTURES // ==================================================== dsSharedPtr<Balloon> georgette(new Balloon("Mr Potato Head")); dsSharedPtr<Balloon> henry(new Balloon("Snoopy")); georgette->addRope2(henry); henry = new Balloon("Tigger"); georgette->addRope2(henry); georgette->print2(); henry->print2(); dsSharedPtr<Balloon> isabelle(new Balloon("Shrek")); henry->addRope2(isabelle); isabelle = new Balloon("Barney the Purple Dinosaur"); georgette->addRope2(isabelle); henry->print2(); georgette->print2(); isabelle->print2(); // // CHECKPOINT 2C: REWRITE THE ABOVE EXAMPLE TO USE SHARED POINTERS // // ==================================================== // CYCLIC STRUCTURES // ==================================================== // FOR CHECKPOINT 3 Balloon* jacob(new Balloon("Dora the Explorer")); Balloon* katherine(new Balloon("Kung Fu Panda")); Balloon* larry(new Balloon("Scooby Doo")); Balloon* miranda(new Balloon("SpongeBob SquarePants")); Balloon* nicole(new Balloon("Papa Smurf")); jacob->addRope(katherine); katherine->addRope(larry); larry->addRope(jacob); miranda->addRope(jacob); nicole->addRope(miranda); larry->addRope(nicole); katherine = NULL; larry = NULL; miranda = NULL; nicole = NULL; // jacob points to a cyclic structure! // to cleanup this structure: deleteAll(jacob); //delete jacob; jacob = NULL; std::cout << "end of main" << std::endl; return 0; // // NOTE: when smart pointers go out of scope, the destructors for // those objects will be called automatically // }