简体   繁体   中英

Program crashes on a specific value when i try to free() the arrays in a pointer array

im trying to use a dynamic array to calculate average with credits but my program crashes when i run with the values:

  • 2 1 1 1 1 3 1 1

it doesn't crash if i do:

  • 2 1 1 1 1 4 1 1 1 1

if i remove the for loop with the free(); inside

for (i=0 ; i<classes ; i++) 
{   //free each individual 2unit array first
    free(grades[i]);    //This line doesnt work
}

it runs fine but i don't want to do that because im told not to do that.

here's the code, i tried to remove the unnecessary parts as best as i can

#include<stdio.h>
#include<stdlib.h>

void fillArray(int **grades,int start,int finish)
{   
    int i;
    for(i=start;i<finish;i++)
    {
        printf("Enter grade for Class %d: ",i+1);
        scanf("%d",&grades[i][0]);
        printf("Enter Credit for Class %d: ",i+1);
        scanf("%d",&grades[i][1]);

    }
}
void expandArray(int **grades,int oldSize,int newSize)
{
    *grades = (int *)realloc(*grades,newSize*sizeof(int*));    //expanding the pointer array
    int i;
    for(i=oldSize;i<newSize;i++)   //filling it with 2 unit arrays per class
    {
        grades[i] = (int *)malloc(2*sizeof(int));   
    }
    fillArray(grades,oldSize,newSize);  
}

int main()
{
    int classes,oldClasses;
    printf("Enter number of classes: ");
    scanf("%d",&classes);

    int **grades = (int **)malloc(classes*sizeof(int*));   //creating an array to store 2unit arrays(pointer array)
    int i;
    for(i=0;i<classes;i++)   //filling the pointer array with 2 unit arrays per class
    {
        grades[i] = (int *)malloc(2*sizeof(int));
    }

    printf("Enter grades for each classes: \n");
    fillArray(grades,0,classes);    // this 0 here means we start at the index 0, that parameter is later used to start at the lastIndex+1


    oldClasses = classes;   // copied the value of classes to oldClasses instead of taking the new one as newClasses to avoid confusion.
    printf("Enter new number of classes: ");
    scanf("%d",&classes);
    expandArray(grades,oldClasses,classes);
    printf("This line works!");
    for (i=0 ; i<classes ; i++) 
    {   //free each individual 2unit array first
        free(grades[i]);    //This line doesnt work
    }
    printf("This won't get printed with the value 3...");
    free(grades);   //free the pointer array (This one also works)

    return 0;
}

Check for value of classes inside main()

When you get number of classed the second time you are reallocating memory within the function expandArray() and this expansion is not visible outside of that function so when you are freeing you might try to free some unallocated memory hence the crash.

expandArray is actually updating grades which is an int** but inside you are updating *grades : You are expanding the first array from 2 grades to newSize grades while you wanted to add a new entire class.

You want to pass the address of the object you want to modify in the function, so you should pass a int*** .

void expandArray(int ***grades,int oldSize,int newSize)
{
    grades = realloc(grades, newSize*sizeof(int*));
    // ...
}

Note: But 3D pointers is usually not a good practice : you might want to modify expandArray so it returns a new int** .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM