So I'm back. I've made some good progress on the program and getting close to finishing.
So, now I'm trying to dynamically allocate some arrays to hold to test scores. I am assigning all values in these arrays to zero. After I get them then to function properly I will uncomment setScore and allow the user to input the test scores.
Here is my code:
This is output so far:Code:#include<iostream> #include<string> using namespace std; class Student { private: string name; int id; int * test; int num; void makeArray() { test = new int[num]; for (int i = 0 ; i < num ; i++) { test[i] = 0; cout << test[i] << endl; } cout << "making array...\n"; } public: Student() { cout << "Calling set functions...\n"; setName("None"); setID(10); num = 3; makeArray(); cout << "creating student...1\n"; } Student(int n) { cout << "Calling set functions...\n"; setName("None"); setID(10); if (n > 0) num = n; else num = 3; makeArray(); cout << "creating student...2\n"; } Student(string nm, int i, int n) { cout << "Calling set functions...\n"; setName(nm); setID(i); if (n > 0) num = n; else num = 3; makeArray(); cout << "creating student...3\n"; } void setName(string nm) { cout << "\nThe Name: "; cin >> nm; name = nm; } void setID(int i) { bool validInput; do { cout << "\nThe ID: "; cin >> i; cout << endl; if ( i >= 10 && i <= 99) { id = i; validInput = true; } else { cout << " Invalid. Can not set id to " << i << " for student " << getName(); cout << endl; id = 10; validInput = false; } } while(!validInput); } // void setScore(int i, int s) // { // // } string getName() const { return name; } int getID() const { return id; } void showScore() { cout << "showing score"; } void display() { cout << "showing display"; } ~Student() { cout << "Freeing dynamicly allocated memory...\n"; delete [] test; } }; int main() { cout << "In main\n"; Student studentA, studentB(4), studentC("Joe",40,5); cout << "\nThe Name: "; cout << studentA.getName() << endl; cout << "The ID: "; cout << studentA.getID() << endl; cout << "\nThe Name: "; cout << studentB.getName() << endl; cout << "The ID: "; cout << studentB.getID() << endl; cout << "\nThe Name: "; cout << studentC.getName() << endl; cout << "The ID: "; cout << studentC.getID() << endl; studentA.~Student(); studentB.~Student(); studentC.~Student(); return 0; }
Code:In main Calling set functions... The Name: Tom The ID: 20 0 0 0 making array... creating student...1 Calling set functions... The Name: John The ID: 30 0 0 0 0 making array... creating student...2 Calling set functions... The Name: Joe The ID: 40 0 0 0 0 0 making array... creating student...3 The Name: Tom The ID: 20 The Name: John The ID: 30 The Name: Joe The ID: 40 Freeing dynamicly allocated memory... Freeing dynamicly allocated memory... Freeing dynamicly allocated memory... Freeing dynamicly allocated memory... *** glibc detected *** /home/john/bin/lab9/Lab9: double free or corruption (fasttop): 0x000000000098e140 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fc267718b96] /home/john/bin/lab9/Lab9[0x4016cf] /home/john/bin/lab9/Lab9[0x401067] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fc2676bb76d] /home/john/bin/lab9/Lab9[0x400d29] ======= Memory map: ======== 00400000-00402000 r-xp 00000000 00:15 43647168 /home/john/bin/lab9/Lab9 00601000-00602000 r--p 00001000 00:15 43647168 /home/john/bin/lab9/Lab9 00602000-00603000 rw-p 00002000 00:15 43647168 /home/john/bin/lab9/Lab9 0098e000-009af000 rw-p 00000000 00:00 0 [heap] 7fc26739e000-7fc267499000 r-xp 00000000 08:01 56102364 /lib/x86_64-linux-gnu/libm-2.15.so 7fc267499000-7fc267698000 ---p 000fb000 08:01 56102364 /lib/x86_64-linux-gnu/libm-2.15.so 7fc267698000-7fc267699000 r--p 000fa000 08:01 56102364 /lib/x86_64-linux-gnu/libm-2.15.so 7fc267699000-7fc26769a000 rw-p 000fb000 08:01 56102364 /lib/x86_64-linux-gnu/libm-2.15.so 7fc26769a000-7fc26784f000 r-xp 00000000 08:01 56102322 /lib/x86_64-linux-gnu/libc-2.15.so 7fc26784f000-7fc267a4e000 ---p 001b5000 08:01 56102322 /lib/x86_64-linux-gnu/libc-2.15.so 7fc267a4e000-7fc267a52000 r--p 001b4000 08:01 56102322 /lib/x86_64-linux-gnu/libc-2.15.so 7fc267a52000-7fc267a54000 rw-p 001b8000 08:01 56102322 /lib/x86_64-linux-gnu/libc-2.15.so 7fc267a54000-7fc267a59000 rw-p 00000000 00:00 0 7fc267a59000-7fc267a6e000 r-xp 00000000 08:01 56102347 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fc267a6e000-7fc267c6d000 ---p 00015000 08:01 56102347 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fc267c6d000-7fc267c6e000 r--p 00014000 08:01 56102347 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fc267c6e000-7fc267c6f000 rw-p 00015000 08:01 56102347 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fc267c6f000-7fc267d54000 r-xp 00000000 08:01 15998324 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17 7fc267d54000-7fc267f53000 ---p 000e5000 08:01 15998324 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17 7fc267f53000-7fc267f5b000 r--p 000e4000 08:01 15998324 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17 7fc267f5b000-7fc267f5d000 rw-p 000ec000 08:01 15998324 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17 7fc267f5d000-7fc267f72000 rw-p 00000000 00:00 0 7fc267f72000-7fc267f94000 r-xp 00000000 08:01 56102300 /lib/x86_64-linux-gnu/ld-2.15.so 7fc268171000-7fc268176000 rw-p 00000000 00:00 0 7fc26818f000-7fc268194000 rw-p 00000000 00:00 0 7fc268194000-7fc268195000 r--p 00022000 08:01 56102300 /lib/x86_64-linux-gnu/ld-2.15.so 7fc268195000-7fc268197000 rw-p 00023000 08:01 56102300 /lib/x86_64-linux-gnu/ld-2.15.so 7fff14746000-7fff14767000 rw-p 00000000 00:00 0 [stack] 7fff147ff000-7fff14800000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted (core dumped)
One thing I would like to point out: It appears to be freeing the dynamically allocated memory an extra time. I dont know if that is realted to the "double free or corruption (out)" error or not but it shoudn't be doing that.
Thanks for all the help.![]()



LinkBack URL
About LinkBacks




