static void PushNode()

in Tutorials/MemoryUsage/Stage2/main.c [126:159]


static void PushNode(Node **headNode)
{
    struct Node *newNode = (Node *)malloc(sizeof(Node));
    if (newNode == NULL) {
        Log_Debug("ERROR: couldn't allocate memory %s (%d)\n", strerror(errno), errno);
        exitCode = ExitCode_AddNode_CreateNode;
        return;
    }

    // Allocate and initialize the memory
    newNode->userData = (int *)calloc(NUM_ELEMS, sizeof(int));
    if (newNode->userData == NULL) {
        Log_Debug("ERROR: couldn't allocate and initialize memory %s (%d)\n", strerror(errno),
                  errno);
        exitCode = ExitCode_AddNode_AllocateUserData;
        free(newNode);
        return;
    }

    listSize++;
    Log_Debug("\nAdding a node to the linked list (list size = %u).\n", listSize);

    if (*headNode == NULL) {
        newNode->next = NULL;
        *headNode = newNode;
        return;
    }

    // The next of the new node is the head of the list
    newNode->next = (*headNode);

    // Move the head to point to the new node
    *headNode = newNode;
}