Home > Enterprise >  Trouble reallocating structure
Trouble reallocating structure

Time:10-08

I am having error massage when I try to reallocate a structure until the user decides to exit. When structure has 3 integers, I am getting "realloc(): invalid next size Aborted (core dumped)" error. But when the structure has only an integer variable, it works fine. Could you help me solve it? Here are my codes:

The code below gives error:

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

typedef struct Students{
    long int id;
    int section;
    int grade;
}Students;
void add_student(Students *myclass,int *count)
{
    printf("\nEnter id,section and grade: ");
    scanf(" %ld %d %d",&myclass[*count].id,&myclass[*count].section,&myclass[*count].grade);
}
int main()
{
    Students *myclass;
    int i,count=0,decision;
    myclass=(Students *) malloc(2*sizeof(Students));
    do{
            printf("To add press 1\nTo exit press 2: ");
            scanf(" %d",&decision);
            if(decision==1){
                    printf("In the main count is %d\n",count);
                    if(count>=2){
                            myclass=(Students *) realloc(myclass,count*sizeof(Students));
                    }
                    add_student(myclass,&count);
                    count =1;
            }
    }while(decision!=2);
    return 0;
}

This code below works fine:

#include<stdio.h>
#include<stdlib.h>
typedef struct Students{
        int grade;
}Students;
void add_student(Students *myclass,int *count)
{
        printf("\nEnter grade: ");
        scanf(" %d",&myclass[*count].grade);
}
int main()
{
        Students *myclass;
        int i,count=0,decision;
        myclass=(Students *) malloc(2*sizeof(Students));
        do{
                printf("To add press 1\nTo exit press 2: ");
                scanf(" %d",&decision);
                if(decision==1){
                        printf("In the main count is %d\n",count);
                        if(count>=2){
                                myclass=(Students *) realloc(myclass,count*sizeof(Students));
                        }
                        add_student(myclass,&count);
                        count =1;
                }
        }while(decision!=2);
        return 0;
}

CodePudding user response:

You've struggled; it's plain to see.

Here's a "clean up" of your code that should get you started.

Call realloc() with an initially NULL pointer and it works just like malloc(). So, instead of estimating 2 to start, start with 0 and grow the array as needed. (Notice the use of 1 and -1 so that the 0th array element is used when there is only 1 array element in existence, 1 when there are 2, etc... Base-0 numbering for the indexes; that last index is 'n-1' when 'n' is the number of elements.

Also, add proper testing of success on all function calls. Don't presume everything will always work as you hope it to.

Finally, pay close attention to the formatting used below. Spread things out so that the code is easier to read. Use more whitespace!

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

typedef struct {
    int grade;
} Student;

void add_student( Student *pOneStudent ) { // NB. Only one student struct here.
    printf( "\nEnter grade: " );
    scanf( "%d", &pOneStudent->grade );
    /* omitting test for failure */
}

int main() {
    Student *myclass = NULL;
    int count = 0, decision;

    do {
        printf(
            "To add press 1\n"
            "To exit press 2: "
        );
        scanf( "%d", &decision );
        /* omitting test for failure */

        if( decision == 1 ) {
            myclass = realloc( myclass, (count   1) * sizeof *myclass );
            /* omitting test for failure */

            count  = 1;
            add_student( myclass   count - 1 ); // base 0 indexing

// print debugging
for( int i = 0; i < count; i   ) printf( "#%d(%d)\n", i, myclass[i].grade );

        }
    } while( decision != 2 );

    // AND, don't forget and have memory leaks.
    // When you are done with "heap storage"...
    free( myclass );

    return 0;
}
To add press 1
To exit press 2: 1

Enter grade: 75
#0(75)
To add press 1
To exit press 2: 1

Enter grade: 68
#0(75)
#1(68)
To add press 1
To exit press 2: 1

Enter grade: 23
#0(75)
#1(68)
#2(23)
To add press 1
To exit press 2: 2
  •  Tags:  
  • c
  • Related