更新全局结构的成员

Updating members of a global struct

我正在尝试用新节点更新全局链表。我使列表成为一个指向结构的指针,每次我尝试为其分配一个新成员值时,我都会收到总线错误 10。我非常关心这个,所以任何帮助将不胜感激。

代码:

typedef struct alarmItem
{
    pthread_t id;  //id of the thread to block/unblock
    int delay;  //initial delay time set by user for this alarm item
    int realdelay;  //adjusted delay time for this item in the queue
    char function[256];  //function for this item to execute once alarm goes off
    char args[256];  //arguments to pass into function, sql query or null
    time_t calltime;  //stores the time that this alarm item was introduced
    struct alarmItem* next;  //stores the next node in the linked list of alarm    items
} alarmItem ;

typedef struct LinkedList
{
    alarmItem* head;
} LinkedList;

LinkedList *alarmq;  //empty linkedlist of alarm items

void initList()
{
    if(alarmq == NULL)
        printf("List is null.\n\n");
    else
        alarmq->head = NULL;
}

void entry_point(char **arguments)
{
    char **args = (char **)arguments;

    //create a new alarm item
    alarmItem *new;

    int d;
    sscanf(args[0], "%d", &d);
    new->delay = d;

    strcpy(new->args, args[3]);
    strcpy(new->function, args[4]);

    initList();
}

entry_point 函数只是从带有标准字符串命令列表的 main 方法中调用。

您需要为 new 结构分配 space,为此您需要 malloc()

void *entry_point(void *data)
{
    alarmItem *new;
    char **args;
    int d;

    args = (char **)data;
    //create a new alarm item
    new = malloc(sizeof(*new));
    if (new == NULL)
        return NULL; /* may be return something else for error handling */
    sscanf(args[0], "%d", &d);
    new->delay = d;

    strcpy(new->args, args[3]);
    strcpy(new->function, args[4]);

    initList();
    return NULL;
}

您可以看到我使您的 entry_point() 函数可以与 pthread_create() 一起使用。

同理alarmq,其实这个条件

if (alarmq == NULL)

在程序的整个生命周期中保持不变,我不明白 initList() 函数应该做什么,但我想它会像

void initList()
{
    if (alarmq == NULL)
    {
        alarmq = malloc(sizeof(*alarmq));
        if (alarmq != NULL)
            alarmq->head = NULL;
    }
}

你的链表 LinkedList 结构也不是真正的链表,你需要在其中包含 next 成员而不是在 alarmItem 结构中。

to start, replace this:

typedef struct LinkedList
{
    alarmItem* head;
} LinkedList;

LinkedList *alarmq;  //empty linkedlist of alarm items

void initList()
{
    if(alarmq == NULL)
        printf("List is null.\n\n");
    else
        alarmq->head = NULL;
}

有了这个:

alarmItem *head = NULL;

大大简化了流程, 从代码中消除了明显的混乱, 并且很容易测试要添加的节点是否是第一个 (第一个节点几乎总是一个特例) 通过:

if( NULL == head )
{ // then, adding first node 
    ...
}
else
{ // else, all other node additions
    ...
}

这段代码(我假设)是如何添加第一个节点的

然而,它有几个问题。

-- 当前代码:

void entry_point(char **arguments)
{
    char **args = (char **)arguments;

    //create a new alarm item
    alarmItem *new;

    int d;
    sscanf(args[0], "%d", &d);
    new->delay = d;

    strcpy(new->args, args[3]);
    strcpy(new->function, args[4]);
    initList();
}

哪个看起来应该更像这样:

(这可以添加任何节点,包括第一个。)

void entry_point(char **args)
{
    alarmItem *newNode = NULL;
    if( NULL == (newNode = malloc( sizeof(alarmItem) )) )
    { // then, malloc failed
        perror( "malloc for alarmItem node failed" );
        cleanup();  // new function to free all allocations
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // amongst other things, this sets newNode->next to NULL
    memset( newNode, 0x00, sizeof(alarmItem) );

    newNode->delay = atoi(args[0]);

    strcpy(newNode->args, args[3]);
    strcpy(newnode->function, args[4]);

    if( NULL == Head )
    { // then, first node to be added
         Head = newNode;
    }

    else
    { // else, append node to end of linked list
        alarmItem *tempNode = Head;
        alarmItem *currentNode = Head;
        while( tempNode->next ) 
        {
            currentNode = tempNode;
            tempNode = tempNode->next;
        } // end while

        // when get here, currentNode points to last node in linked list
        currentNode->next = newNode; 
    } // end if    
} // end function: entry_point