1

Im having trouble with the syntax here. We are studying structures and pointers in class currently and are tasked with creating a dynamic array of a single structure with a pointer array inside to both be allocated and deleted by the end of the program. (Hopefully that made sense)

Here are the snippits of code im working with, note how the entry of scores works:

std::cin << stuArray[i].stuScore[j]

But then the deletion in a similar manner, does not:

delete[] stuArray[count].stuScore[j];

Deletion Code:

    do
{
    for (unsigned short j = 0; j < numTests; j++)
    {
        delete[] stuArray[count].stuScore[0]; //Syntax???????
    }
    count++;
} while (count < numStudents);
delete[] stuArray;

Score Entry Code (Which Works)

bool ScoreEntry(Student * stuArray, unsigned short numStudents, unsigned short numTests)
{
    //Local Variables
    unsigned short idTempChoice = 0;

    //Get Id Number
    std::cout << "\nChoose a student by ID and enter the test scores: ";
    std::cin >> idTempChoice;

    //Id lookup
    for (unsigned short i = 0; i < numStudents; i++)
    {
        //Id Check
        if (idTempChoice == stuArray[i].stuId)
        {
            std::cout << "Student selected: " << stuArray[i].stuName << std::endl;

            //Score Entry
            for (unsigned short j = 0; j < numTests; j++)
            {
                std::cout << "Test " << j + 1 << "'s Score: ";
                std::cin >> stuArray[i].stuScore[j];
            }//End For Loop j

            return true;
        }
    }//End For Loop i

    //Student Id not found
    std::cout << "Student not found!\n";
    return false;
}

Allocation Code (Struct):

void MemAllocation(Student * &stuArray, unsigned short &numStudents)
{
    //Get Number of students
    std::cout << "How many students have taken the test: ";
    std::cin >> numStudents;
    std::cout << std::endl;

    //Dynamically allocate pointers
    stuArray = new Student[numStudents];
}

Allocation Code (Pointer inside struct):

for (unsigned short i = 0; i < numTests; i++) //Allocate Dynamic array for each student
    {
        stuArray[i].stuScore = new float[numTests];
    }

This is Literally all the code you need reference, this is not a bug its a syntax problem :)

6
  • 2
    You have allocated stuArray[count].stuScore with new[], but not stuArray[count].stuScore[j]. Only delete[] what you actually new[]. Commented May 8, 2018 at 3:27
  • 1
    You can't delete individual elements of an array you allocated with new. You can only delete the entire array. It's all or none. Commented May 8, 2018 at 3:30
  • is 'stuArray[i].stuScore = new float[numTests];' not doing just that? I assume this would create a new array of stuScores such that I can enter data in with a counter and delete with a counter? Commented May 8, 2018 at 3:31
  • @drescherjm So if I were to delete stuArray[count].stuScore that would be enough? It makes sense because I did the same with stuArray... ha Commented May 8, 2018 at 3:32
  • You can delete delete[] stuArray[count].stuScore but not delete[] stuArray[count].stuScore[j] Commented May 8, 2018 at 3:32

2 Answers 2

1

Try delete[] stuArray[count].stuScore;

not delete[] stuArray[count].stuScore[j];

delete [] is made to delete an array allocated with new type[n]

You want to delete the pointer to the memory, not the actual memory.

Sign up to request clarification or add additional context in comments.

Comments

0

You can delete delete[] stuArray[count].stuScore but not delete[] stuArray[count].stuScore[j] - drescherjm

Fixed using:

do
{
    delete[] stuArray[count].stuScore;
    count++;
} while (count < numTests);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.