user3900395
user3900395

Reputation: 53

C++ struct pointer and memory leak

I am doing a c++ book question and got stucked on this specific parts where it says

"write program that asks how many score there are and how many student there are. it should then dynamically allocate an array of structures, each structure's Test member should point to a dynamically allocated array that will hold the test scores,after array has been dynamically allocated the program should ask for the id number and test score for each student".

right now I have a problem in the for loop where there is a memory leak and program crashes after input values, any suggestions ?

here is the code:

struct Course
{
      string Name;
      int IdNumber;
      int *Tests;
      int Average;
      int courseGrade;
};

void Result(int,int );

int main()
{
    cout<<"please enter number of test score ";
     int testScore;
     cin>>testScore;
     int numberofStudents;
     cout<<"please enter number of student there is ";
     cin>>numberofStudents;
     Result(numberofStudents,testScore);
}

void Result(int numberofStudents,int testScore)
{
      const int Size=numberofStudents;
      const int Size1=testScore;
      Course *object=nullptr;
      object=new Course[numberofStudents];
      object->Tests = new int[testScore];
      for(int i=0;i<testScore;i++)
      {
           cin>>object[i].Tests[i];
      }
}

here is the input on the console

please enter number of the test scores :3
please enter number of students there is :3
34
90

the program crashes after I input 90

Upvotes: 1

Views: 1047

Answers (3)

Naseef Chowdhury
Naseef Chowdhury

Reputation: 2464

I have made some changes in your code, and it doesn't crash anymore. Why you need to use pointer of Test. I think it is good to use static memory instead of dynamic memory. Please check this

#include<iostream>
#include<cstring>
using namespace std;

struct Course
{
      string Name;
      int IdNumber;
      int Tests[100];
      int Average;
      int courseGrade;
};

void Result(int,int );

int main()
{
    cout<<"please enter number of test score ";
     int testScore;
     cin>>testScore;
     int numberofStudents;
     cout<<"please enter number of student there is ";
     cin>>numberofStudents;
     Result(numberofStudents,testScore);
}

void Result(int numberofStudents,int testScore)
{
      const int Size=numberofStudents;
      const int Size1=testScore;
      Course *object=nullptr;
      object=new Course[numberofStudents];
      //object->Tests = new int[testScore];
      for(int i=0;i<testScore;i++)
      {
           cin>>object[i].Tests[i];
      }
}

Upvotes: 0

James Curran
James Curran

Reputation: 103515

There is a memory leak because you are allocating things withj new and never freeing them with delete.

As for the crash, it's being caused by this line:

  object->Tests = new int[testScore];

Remember that object isn't a Course object, it's an array of Course objects, every one of which needs it's own Tests array. The line is effectively just object[0].Tests = ...

So You need a loop on numberofStudentsto allocate the test in each Course, and the another loop on numberofStudents around the loop on testScore (which you already have) to gather all the grades. (Advanced study: you can combine those two loops)

Upvotes: 0

Ranic
Ranic

Reputation: 486

Here's what I suspect is happening:

In this for loop:

  for(int i=0;i<testScore;i++)
  {
       cin>>object[i].Tests[i];
  }

You access object using testScore as an index. If testScore is larger than the length of object you will run into issues.

The memory leak problem comes from the fact that you are allocating space for object and every Tests member of a Course but you never free that memory.

Upvotes: 1

Related Questions