// Ex752.cc (derived from Ex643-correct.cc) // Sec. 7.5.2 of lecture // Dynamic arrays in structures - header file // Function: CopyStudent2 from student.hh // g++ -c student.cc // g++ -c Ex752.cc // g++ Ex752.o student.o // or for §7.5.3 // g++ -c student.cc // ar r libstud.a student.o // g++ Ex752.cc -L. -lstud #include #include // setw #include // strcpy, strlen #include "student.hh" main() { Student2 arni, robbi; char tmp[20]; // temp. input string // --------------------------------------------------------------- // Input pvorname cout << endl << " Vorname : "; // setw guarantes that not more than 20 characters are read // see Schader/Kuhlin, p.96 cin >> setw(sizeof(tmp)) >> tmp; // Allocate memory for arni.pvorname arni.pvorname = new char[strlen(tmp)+1]; strcpy(arni.pvorname,tmp); // and copy input on it // --------------------------------------------------------------- // Input pname cout << endl << " Familienname : "; cin >> setw(sizeof(tmp)) >> tmp; // Allocate memory for arni.pname arni.pname = new char[strlen(tmp)+1]; strcpy(arni.pname,tmp); // and copy input on it // --------------------------------------------------------------- // Input skz cout << endl << " Studentenkennzahl : "; cin >> arni.skz; // --------------------------------------------------------------- // Input matrikel cout << endl << " Matrikelnummer : "; cin >> arni.matrikel; // --------------------------------------------------------------- // correct copying Copy_Student2(robbi,arni); // robbi = arni; // copies int, long long int, int* // // but pvorname, pname point on dynamical data are posessed by arni // // ===> allocate dynamical memory for robbi // // (=>redefinition of pvorname, pname) // // // Allocate memory for robbi.pname // // robbi.pname = new char[strlen(arni.pname)+1]; // strcpy(robbi.pname,arni.pname); // and copy input on it // // // Allocate memory for robbi.pvorname // // robbi.pvorname = new char[strlen(arni.pvorname)+1]; // strcpy(robbi.pvorname,arni.pvorname); // and copy input on it // --------------------------------------------------------------- // output robbi cout << endl << "-------- This output is still correct -----------" << endl; cout << robbi.pvorname << " " << robbi.pname << ", SKZ: "; cout << robbi.skz << " " << robbi.matrikel << endl << endl; // --------------------------------------------------------------- // Now, we deallocate dynamical data in arni delete [] arni.pvorname; delete [] arni.pname; // --------------------------------------------------------------- // output robbi cout << endl << "-------- Even this output will be correct -----------" << endl; cout << robbi.pvorname << " " << robbi.pname << ", SKZ: "; cout << robbi.skz << " " << robbi.matrikel << endl << endl; // --------------------------------------------------------------- // Let us allocate in init some tiny dynamical array char *tiny; tiny = new char [5]; strcpy(tiny,"tiny"); // --------------------------------------------------------------- // output robbi // Suddenly, robbi.pname == "tiny" cout << endl << "-------- Hey, nothing happend to my output!! -----------" << endl; cout << robbi.pvorname << " " << robbi.pname << ", SKZ: "; cout << robbi.skz << " " << robbi.matrikel << endl << endl; // --------------------------------------------------------------- // Now, we deallocate dynamical data in robbi delete [] robbi.pvorname; delete [] robbi.pname; }