Hello,
I'm new to dynamic memory in C++ and am working on a terminal program that's meant to act as a grade calculator. A user can add grades and the program will calculate their average GPA as well as display all the grades they entered.
To add the grades I'm using parallel dynamic arrays (one for the number score and one for the corresponding letter grade) that should technically grow infinitely. The code I have works, but after adding 8 elements in the array i get the following error:
This is what it looks like when running my program as is, as you can see it displays the first 7 elements of my array:main: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) &7 ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
Main Menu:
1. Add Grade
2. Display All Grades
3. Process All Grades
4. Quit Program
1
Add a grade: 42
You added 42.
Main Menu:
1. Add Grade
2. Display All Grades
3. Process All Grades
4. Quit Program
2
# Score Grade
0 84 B
1 95 A
2 82 B
3 75 C
4 96 A
5 95 A
6 42 F
Main Menu:
1. Add Grade
2. Display All Grades
3. Process All Grades
4. Quit Program
1
Carroll: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
Here's my function code:
And here's how I'm calling the function in main:Code:int* addGrade (int* array, int size) { int* pTemp = nullptr; int temp; //temp array for conditional pTemp = new int[size];//create new array that will have size of size //loop to copy over array for (int i=0; i < size; i++) { pTemp[i]=array[i]; } cout << endl << "Add a grade: "; cin >> temp; //if score isn't between 0-100, have them re-enter while ( temp > 100 || temp < 0 ) { cout << endl << "That's an invalid input, please make sure your score is between 0-100." << endl << "Add a grade: "; cin >> temp; } pTemp[size] = temp; //assign score to new array element delete[] array; array = pTemp; //grade array now pointing at temp pTemp = nullptr; delete[] pTemp; cout << endl << "You added " << array[size] << "." << endl; return(array);//return new address of array }
I have tried using valgrind but I don't quite understand what it means, if anyone does this is what it outputs after the 1st addition to the grade array:Code://Add a grade if (retval == 1) { pGrade = addGrade(pGrade, size); //assign new ptr address to pGrade on return pLetterGrade = addLetterGrade(pLetterGrade, pGrade, size); size++; //increase size of arrays retval=Menu(); //display menu again }
Any assistance is appreciated.Main Menu:
1. Add Grade
2. Display All Grades
3. Process All Grades
4. Quit Program
1
Add a grade: 54
==12110== Invalid write of size 4
==12110== at 0x10999C: addGrade(int*, int) (Carroll.cpp:226)
==12110== by 0x1092FD: main (Carroll.cpp:100)
==12110== Address 0x5b7e580 is 0 bytes after a block of size 0 alloc'd
==12110== at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12110== by 0x109895: addGrade(int*, int) (Carroll.cpp:207)
==12110== by 0x1092FD: main (Carroll.cpp:100)
==12110==
==12110== Invalid read of size 4
==12110== at 0x109A0F: addGrade(int*, int) (Carroll.cpp:233)
==12110== by 0x1092FD: main (Carroll.cpp:100)
==12110== Address 0x5b7e580 is 0 bytes after a block of size 0 alloc'd
==12110== at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12110== by 0x109895: addGrade(int*, int) (Carroll.cpp:207)
==12110== by 0x1092FD: main (Carroll.cpp:100)
==12110==
You added 54.
==12110== Invalid read of size 4
==12110== at 0x109B30: addLetterGrade(char*, int*, int) (Carroll.cpp:288)
==12110== by 0x109317: main (Carroll.cpp:101)
==12110== Address 0x5b7e580 is 0 bytes after a block of size 0 alloc'd
==12110== at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12110== by 0x109895: addGrade(int*, int) (Carroll.cpp:207)
==12110== by 0x1092FD: main (Carroll.cpp:100)
==12110==
==12110== Invalid write of size 1
==12110== at 0x109B47: addLetterGrade(char*, int*, int) (Carroll.cpp:288)
==12110== by 0x109317: main (Carroll.cpp:101)
==12110== Address 0x5b7e5c0 is 0 bytes after a block of size 0 alloc'd
==12110== at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12110== by 0x109AE4: addLetterGrade(char*, int*, int) (Carroll.cpp:279)
==12110== by 0x109317: main (Carroll.cpp:101)
==12110==
Thank you!