我试图用新节点更新全局链表。我使列表成为一个结构的指针,每次我尝试为它分配一个新的成员值我得到一个总线错误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();
}
使用标准的字符串命令列表从main方法调用entry_point函数。
答案 0 :(得分: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
成员,而不是alarmItem
结构中的成员。
答案 1 :(得分: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
...
}
答案 2 :(得分:0)
这段代码(我假设)是如何添加第一个节点的
然而,它有几个问题。- 当前代码:
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