0

This program compiles fine, but I can't get it to run. Every time I try to run it I get a "segmentation fault (core dump)". I think it has something to do with allocating the memory or declaring the array. I tried to declare the array with one *, but then it won't compile. How do I fix this?

struct classStats 
{
    float mean;
    float min;
    float max;
    float median;
    char *name;
};

int main ()
{
    int i=19;
    string line;
    classStats class_data;
    student **array; 
    float count;
    class_data.max = 100;
    class_data.min = 0;
    class_data.median = array[10] -> mean;

    array = (student**) malloc(i* sizeof(student*));
    cin >> line; 

    for (int j=0; j<i; j++)
    {
        array[j] = (student*) malloc(sizeof(student));

        cin >> array[j] -> first;
        cin >> array[j] -> last;
        cin >> array[j] -> exam1;
        cin >> array[j] -> exam2;
        cin >> array[j] -> exam3; 

        array[j] -> mean  = ((array[j]-> exam1 + array[j] -> exam2 + array[j] -> exam3) / 3);
        cout << array[j] -> mean;
    } // end of for

    bubble(array, i);
    count = 0;
    for (int j=0; j<i; j++)
    {
        count = count + array[j]->mean;
        if(array[j]-> mean < class_data.min)
        {
            class_data.min = array[j]->mean;
        }//end of if
        else if(array[j]->mean < class_data.max)
        {
            class_data.max = array[j]->mean;
        } // end of else if 
    }
    class_data.mean = count / i;

    cout << line << class_data.mean << class_data.min << class_data.max << class_data.mean;
    for (int j=0; j<19; j++)
    {
        cout << *array[j]->first << *array[j]->last << array[j]->mean << endl; 
    }

    free(array);
    return 0;
} // end of main
Vishaal Shankar
  • 1,648
  • 14
  • 26
  • 2
    You really do not need `student **array;`. You need an array of students, which is `student *array;` (_one_ asterisk). The program should be rewritten accordingly. – DYZ Feb 08 '18 at 05:06
  • You should use new-delete instead of malloc-free in C++ ! – Vishaal Shankar Feb 08 '18 at 05:19
  • The program does not compile. No #include files. `bubble` undefined, etc. etc... It needs to be in a form such that we can cut and paste it into a file and compile it as is. Read how to present a [MCVE]. Now then. Do not use `malloc`, `free`, `operator new`, or `operator delete`. Try to avoid pointers. Use std::vector for the array. – Jive Dadson Feb 08 '18 at 06:27

2 Answers2

1

Please have look at following line in your code

class_data.median = array[10] -> mean;

You are trying to access memory location before you allocate memory to array.

Prasad Bhokare
  • 349
  • 1
  • 2
  • 12
  • Okay, I moved this, but I still have a segmentation dump. – Tien Huynh Feb 08 '18 at 05:41
  • Remove "*" from these lines. for (int j=0; j<19; j++) { cout << *array[j]->first << *array[j]->last << array[j]->mean << endl; } – Prasad Bhokare Feb 08 '18 at 06:05
  • I ran your code successfully after removing * from given lines. But I had modified your code to remove "buuble()" from the code. Please give exact lines where you are getting the fault. – Prasad Bhokare Feb 08 '18 at 06:07
0

Right here:

class_data.median = array[10] -> mean;

You are dereferencing a pointer that does not point to an actual location in memory, as you call malloc for the array of pointers, then malloc for each pointer in the array after attempting to dereference array in the aforementioned line of code. Initialize median after initializing each student pointer.

Also, since you're using C++, use the new[] operator, like:

array = new student*[19];

EDIT: Grammar

  • Operator new is radioactive. Use std::vector student_array(19); – Jive Dadson Feb 08 '18 at 06:29
  • That is certainly an option, and one I would prefer to use myself. Some users might not be able to use the std library, or want to use lower level features of C++ though. – frankmackey Feb 08 '18 at 06:31
  • Operator new is toxic. Everyone has the STL. – Jive Dadson Feb 08 '18 at 06:35
  • https://stackoverflow.com/questions/48676953/incompatible-types-in-assignment-when-passing-custom-function-type/48677047#48677047 – frankmackey Feb 08 '18 at 06:35
  • I agree though. I wouldn't touch operator new unless I knew I would be utilizing it carefully and making sure I managed the scope of the pointer properly. – frankmackey Feb 08 '18 at 06:36