2015-02-09 44 views
0

我试图用新节点更新全局链接列表。我做了一个指向结构体的指针,每次我尝试为它分配一个新的成员值时,我得到一个总线错误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(); 
} 

的入口点函数只是被从与字符串的命令的标准列表的主要方法调用。

+0

这使得绝对没有感觉'char ** args =(char **)参数;'。 – 2015-02-09 23:39:39

+1

'alarmItem * new;'你没有为你的变量分配内存,例如使用malloc - 最好避免保留的C++单词,比如新的,即使在纯c中 – 2015-02-09 23:39:41

+0

@Lashane如果OP没有使用C++,没有理由这么说。事实上,它可以防止错误地用C++编译器编译。 – 2015-02-09 23:40:07

回答

1

您需要为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成员,而不是在有它的结构。

+0

谢谢你,我甚至没有想到这一点。我感谢您的帮助! – colinmcp 2015-02-10 00:06:16

0
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 
    ... 
} 
0

这个代码是(我assumi ng)如何添加第一个节点

但是,它有几个问题。

- 当前代码:

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