2015-02-09 47 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