0

I am using VScode as my IDE. So I created an array, and each element of the array stores a pointer to a linked list. My code is shown as below:

typedef struct AdjStopList
{
    char stopName[20]; //name of stop bus is equal or less than 20 characters
    int numOfAdjStp;   //number of adjacent stops to this stop
    struct BusAtStopList *buslist;  // store all the buses passing this stop in a linked list
    struct AdjStopNode *first; //pointed at the first AdjBusStop of the linked list
    struct AdjStopNode *last;  //pointed at the first AdjBusStop of the linked list
} AdjStopList;

typedef struct BusNetwork
{
    int nBusStop; //number of bus stops in the newwork
    struct AdjStopList **array;
} BusNetwork;

//create a new empty AdjBusStopList
AdjStopList *newAdjStopList()
{
    AdjStopList *newList = (AdjStopList *)malloc(sizeof(AdjStopList));
    newList->buslist = newBusAtStopList();
    assert(newList != NULL);
   // memset(newList, NULL, 20 * sizeof(newList[0]));
    newList->first = NULL;
    newList->last = NULL;
    newList->numOfAdjStp = 0;
    return newList;
}

I tried to use a while loop to assign stopName with "test" in a function

BusNetwork *newBusNetwork(int n, const char *BusStops)
{
    newBN->array = malloc(n * sizeof(AdjStopList*)); 

    for (int i = 0; i < 10; i++)
    {
        newBN->array[i] = newAdjStopList();
        strcpy(newBN->array[i]->stopName, "test");
    }
        
    //rest of the function
}

Then when I watch the variable (struct AdjStopList (**)[46])newBN->array in VScode, it seems only the first element of the array has been processed correctly, e.g. newBN->array[0]->stopName is test. The rest elements of the array still have random garbage values, e.g. newBN->array[1]->stopName.

Can anyone see why this is happening, please?

1

1 Answer 1

1

The initial value of buffer allocated via malloc() is indeterminate and using the (indeterminate) value invokes undefined behavior.

You have to initialize the pointers before using them.

Also note that the elements of the array pointed at by newBN->array is struct AdjStopList *, so sizeof(struct AdjStopList *) or sizeof(newBN->array[0]) should be multiplied instead of sizeof(AdjStopList).

BusNetwork *newBusNetwork(int n, const char *BusStops)
{
    /* correct allocation size */
    newBN->array = malloc(n * sizeof(newBN->array[0])); 
    /* check if allocation succeeded */
    if (newBN->array == NULL) return NULL;

    /* make sure not to cause out-of-range access */
    for (int i = 0; i < n && i < 10; i++)
    {
        /* initialize the element */
        newBN->array[i] = malloc(sizeof(*newBN->array[i]));
        /* check if allocation succeeded */
        if (newBN->array[i] != NULL)
        {
            strcpy(newBN->array[i]->stopName, "test");
        }
    }
        
    //rest of the function
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note that in relation to OP's previous question, where you initialize the element, you would call your newAdjStopList instead of malloc because that would allocate the memory.
Sorry I have edited my question to include the malloc. It still causes me the same problem though....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.