c-更新全局结构的成员



我正在尝试用一个新节点更新全局链表。我让列表成为一个指向结构的指针,每次我试图给它分配一个新的成员值时,我都会得到一个总线错误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.nn");
    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函数只是通过一个标准的字符串命令列表从主方法调用的。

您需要为new结构分配空间,为此您需要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.nn");
    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

相关内容

  • 没有找到相关文章

最新更新