访问非空结构成员时出现分段错误-
Segmentation fault while access non null struct member-
我在将结构成员与 0 进行比较时出现分段错误。
令人困惑的部分是比较确实发生了几次然后暗恋。
请帮忙。
代码:
Dword addup(void)
{
Symbol *curr=NULL;
Dword ans=0;
plast->next=NULL;
if( (phead==NULL) || (plast==NULL) )/*checks that the global pointer to the head & tail is not null.*/
return 0;
curr=phead;
printf("point 1 in addup()********\n");
for( ; curr!=NULL ; curr=curr->next )
{
if(curr!=NULL)
{
if( ((curr->feature.oper)==0) && ((curr->feature.ext)==0) )/*crash here !*/
{
puts("point 2AA in addup()********");
curr->adess=+IC;
ans++;
}
puts("point 2B in addup()********");
}
if((curr->next)==NULL)
break;
}
if( (curr!=NULL) && (plast!=NULL) )
{
if(curr==plast)/*meaning all the list been searched.*/
return ans;
}
return (-1); }
和符号:
typedef struct snode {
char label[MAXLABEL];
Dword adess;
struct
{
unsigned int ext:1;
unsigned int oper:1;
}feature;
struct snode *next;
}Symbol;
感谢所有帮助过的人!
**第一次编辑-
phead 是指向列表头部的指针。
plast 是指向列表最后一个节点(Symbol)的指针。
在其他函数中初始化。
plast->next 始终为 null。(这是在函数的开头 - 因为我想确定)。
**第二次编辑-
创建函数 -
void csymbl(Dword addess, char *name, Dword ext, Dword ope ){
tempnode.adess=addess;/*tempnode is a global Symbol,that been used to insert new nodes.*/
strcpy(tempnode.label,name);
tempnode.feature.ext=ext;
tempnode.feature.oper=ope;
tempnode.next=NULL;}
以及将其连接到列表的函数:
void addsymbol(Symbol a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=&a;
plast=phead;
}
else if(plast==phead)/*If plast point to phead,than there is only one symbol in the list(up to now).*/
{
plast=&a;
phead->next=plast;
plast->next=NULL;
}
else
{
plast->next=&a;
plast=plast->next;
plast->next=NULL;
}}
您没有为第一个元素初始化 next
void addsymbol(Symbol a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=&a;
plast=phead;
// unless set it can have any value
plast->next = NULL;
}
....
}
此外,您应该像这样将 a
作为指针传递:
void addsymbol(Symbol* a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=a; // Note this changed - no & operator
正如评论中已经提到的,这里也存在问题:
Dword addup(void)
{
Symbol *curr=NULL;
Dword ans=0;
// plast->next=NULL; Don't do it here.... plast may be NULL
if( (phead==NULL) || (plast==NULL) )/*checks that the global pointer to the head & tail is not null.*/
return 0;
plast->next=NULL; // If you really want that code, do it here
最后我想知道你是否真的想要这个:
curr->adess=+IC;
或这个
curr->adess+=IC;
你的addsymbol
函数有一个非常的严重问题,会导致未定义的行为:
void addsymbol(Symbol a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=&a;
plast=phead;
}
...
}
在上面的代码中,如果 phead
是 NULL
你让它指向 local 变量a
.
局部变量之所以称为local,是因为它们在定义它们的函数内部是局部的,一旦一个函数returns局部变量就不复存在了。局部变量 lifetime 仅适用于定义它的范围。参数与其他局部变量没有区别。
如果您存储指向此类局部变量的指针,则该指针将变得无效,并且当您使用该指针时,您将有未定义的行为。
我在将结构成员与 0 进行比较时出现分段错误。 令人困惑的部分是比较确实发生了几次然后暗恋。
请帮忙。 代码:
Dword addup(void)
{
Symbol *curr=NULL;
Dword ans=0;
plast->next=NULL;
if( (phead==NULL) || (plast==NULL) )/*checks that the global pointer to the head & tail is not null.*/
return 0;
curr=phead;
printf("point 1 in addup()********\n");
for( ; curr!=NULL ; curr=curr->next )
{
if(curr!=NULL)
{
if( ((curr->feature.oper)==0) && ((curr->feature.ext)==0) )/*crash here !*/
{
puts("point 2AA in addup()********");
curr->adess=+IC;
ans++;
}
puts("point 2B in addup()********");
}
if((curr->next)==NULL)
break;
}
if( (curr!=NULL) && (plast!=NULL) )
{
if(curr==plast)/*meaning all the list been searched.*/
return ans;
}
return (-1); }
和符号:
typedef struct snode {
char label[MAXLABEL];
Dword adess;
struct
{
unsigned int ext:1;
unsigned int oper:1;
}feature;
struct snode *next;
}Symbol;
感谢所有帮助过的人!
**第一次编辑- phead 是指向列表头部的指针。 plast 是指向列表最后一个节点(Symbol)的指针。 在其他函数中初始化。 plast->next 始终为 null。(这是在函数的开头 - 因为我想确定)。
**第二次编辑- 创建函数 -
void csymbl(Dword addess, char *name, Dword ext, Dword ope ){
tempnode.adess=addess;/*tempnode is a global Symbol,that been used to insert new nodes.*/
strcpy(tempnode.label,name);
tempnode.feature.ext=ext;
tempnode.feature.oper=ope;
tempnode.next=NULL;}
以及将其连接到列表的函数:
void addsymbol(Symbol a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=&a;
plast=phead;
}
else if(plast==phead)/*If plast point to phead,than there is only one symbol in the list(up to now).*/
{
plast=&a;
phead->next=plast;
plast->next=NULL;
}
else
{
plast->next=&a;
plast=plast->next;
plast->next=NULL;
}}
您没有为第一个元素初始化 next
void addsymbol(Symbol a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=&a;
plast=phead;
// unless set it can have any value
plast->next = NULL;
}
....
}
此外,您应该像这样将 a
作为指针传递:
void addsymbol(Symbol* a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=a; // Note this changed - no & operator
正如评论中已经提到的,这里也存在问题:
Dword addup(void)
{
Symbol *curr=NULL;
Dword ans=0;
// plast->next=NULL; Don't do it here.... plast may be NULL
if( (phead==NULL) || (plast==NULL) )/*checks that the global pointer to the head & tail is not null.*/
return 0;
plast->next=NULL; // If you really want that code, do it here
最后我想知道你是否真的想要这个:
curr->adess=+IC;
或这个
curr->adess+=IC;
你的addsymbol
函数有一个非常的严重问题,会导致未定义的行为:
void addsymbol(Symbol a){
if(phead==NULL)/*if phead point to NULL - meaning this is the first symbol been entered & up now the linked list was empty. */
{
phead=&a;
plast=phead;
}
...
}
在上面的代码中,如果 phead
是 NULL
你让它指向 local 变量a
.
局部变量之所以称为local,是因为它们在定义它们的函数内部是局部的,一旦一个函数returns局部变量就不复存在了。局部变量 lifetime 仅适用于定义它的范围。参数与其他局部变量没有区别。
如果您存储指向此类局部变量的指针,则该指针将变得无效,并且当您使用该指针时,您将有未定义的行为。