1

I am a begginer programmer trying to do a project for a class on college that requires the use of dynamic strutures. My problem: I use realloc to increase the size of my struture and it works the first time, but not on the second. the auxiliary pointer wich I use to recieve the new increased struture seems to get the value NULL on the second time.

Here's my code so far:

funtion where I try to increase structure:

TList *addToQueue (TList *plist, int *counter, char departure)
{
    TList *auxiliary;
    auxiliary = NULL;

    auxiliary = realloc(plist,(*counter+1)*sizeof(TList));

    if (auxiliary == NULL)
    {
        printf("Insufficient Memory");
        cleanstdin();
    }
    else
    {
        plist = auxiliary;
        plist[*counter].departure = departure;

        do{
        printf("\nPoint of Arrival: ");
        plist[*counter].arrival = checkMenuOption(OPTIONSLOC);
        }while(plist[*counter].arrival == '\0');

        printf("\nNumber of passengers:");
        plist[*counter].numberOfPassengers = readIntenger(MIN_SEATS, MAX_SEATS);

        (*counter) ++;

        printf("\nTicket Purchased successfully");
        cleanstdin();

    }
    return plist;
}

How I'ts declared above main:

TList *addToQueue (TList *plist, int *counter, char departure);

How I call in main:

char departure;

            departure = sellTicketsMenu ();

            switch (departure)
            {
            case 'A':
            {
                ListA = addToQueue (ListA, &counterA, departure);
                cleanstdin();
            }
            break;
            case 'B':
            {
                ListB = addToQueue (ListB, &counterB, departure);
                cleanstdin();
            }
            break;
            case 'C':
            {
                ListC = addToQueue (ListC, &counterC, departure);
                cleanstdin();
            }
            break;
            }

Other notes: readintenger - basic funtion to read intenger from keyboard cleanstdin - basic clear standarin function checkmenuoption - funtion to check the option from menus counters - counters to help with the index of the struture

If you need anything else to understand or to help me please do let me know. Thanks in advance.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97

2 Answers2

1
  1. Better to post here rather than off-site

  2. Off-site code (posted in deleted answer) calls reallloc(), via addToQueue(), with a pointer that has all ready been free'd.

     case 'C':
       ...
       free(ListA);
       switch (optionMTI) {
         case 'A': {
           ...
           ListA = addToQueue (ListA, &counterA, departure);
    

Problem is re-use of a free'd pointer.
If code is to re-use a pointer after free(), be sure to assign it to NULL.

            // Fixed code  
            free(ListA);
            ListA = NULL;  // add
            ...
            ListA = addToQueue (ListA, &counterA, departure);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

The only possible explanation which cannot be confirmed because of the lack of source code is that the passed parameter counter is not initialized or list is not set to NULL before the first call.

You must initialize it to 0, a typical for loop would look like

int counter;
int i;
int numberOfStructs;
TList *list;

counter = 0; /* <--- this is the important part */
numberOfStructs = SOME_NUMBER_MAY_BE_FROM_INPUT;
list = NULL;
for (i = 0 ; i < numberOfStructs ; ++i)
    list = addToQueue (list, &counter, departure);

in your case you could just try

int counterA = 0;
int counterB = 0;
int counterC = 0;

at the counter* variables definition.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97