至少在我看来很奇怪..。程序在我第四次调用enter()函数后运行normally.But,有一个分段错误!如果有任何帮助,我将不胜感激。
使用下面的函数enter(),我想将用户命令的数据添加到一个列表中。
代码的某些部分已经发布在我的另一个问题上,但我认为我应该发布它again...as这是我现在面临的另一个问题。
/* struct for all the datas that user enters on file*/
typedef struct catalog
{ char short_name[50];
char surname[50];
signed int amount;
char description[1000];
struct catalog *next;
}catalog,*catalogPointer;
catalogPointer current;
catalogPointer head = NULL;
void enter(void) //user command: i <name> <surname> <amount> <description>
{
int n,j=2,k=0;
char temp[1500];
char *short_name,*surname,*description;
signed int amount;
char* params = strchr(command,' ') + 1; //strchr returns a pointer to the 1st space on the command.U want a pointer to the char right after that space.
strcpy(temp, params); //params is saved as temp.
char *curToken = strtok(temp," "); //strtok cuts 'temp' into strings between the spaces and saves them to 'curToken'
printf("temp is:%s \n",temp);
printf("\nWhat you entered for saving:\n");
for (n = 0; curToken; ++n) //until curToken ends:
{
if (curToken)
{ short_name = malloc(strlen(curToken) + 1);
strncpy(short_name, curToken, sizeof (short_name));
}
printf("Short Name: %s \n",short_name);
curToken = strtok(NULL," ");
if (curToken)
{ surname = malloc(strlen(curToken) + 1);
strncpy(surname, curToken,sizeof (surname)); }
printf("SurName: %s \n",surname);
curToken = strtok(NULL," ");
if (curToken)
{ //int * amount= malloc(sizeof (signed int *));
char *chk;
amount = (int) strtol(curToken, &chk, 10);
if (!isspace(*chk) && *chk != 0)
fprintf(stderr,"Warning: expected integer value for amount, received %s instead\n",curToken);
}
printf("Amount: %d \n",amount);
curToken = strtok(NULL,"\0");
if (curToken)
{ description = malloc(strlen(curToken) + 1);
strncpy(description, curToken, sizeof (description));
}
printf("Description: %s \n",description);
break;
}
if (findEntryExists(head, surname,short_name) != NULL) //call function in order to see if entry exists already on the catalog
printf("\nAn entry for <%s %s> is already in the catalog!\nNew entry not entered.\n",short_name,surname);
else
{
printf("\nTry to entry <%s %s %d %s> in the catalog list!\n",short_name,surname,amount,description);
newEntry(&head,short_name,surname,amount,description);
printf("\n**Entry done!**\n");
}
// Maintain the list in alphabetical order by surname.
}
catalogPointer findEntryExists (catalogPointer head, char num[],char first[])
{ catalogPointer p = head;
while (p != NULL && strcmp(p->surname, num) != 0 && strcmp(p->short_name,first) != 0)
{ p = p->next; }
return p;
}
catalogPointer newEntry (catalog** headRef,char short_name[], char surname[], signed int amount, char description[])
{
catalogPointer newNode = (catalogPointer)malloc(sizeof(catalog));
catalogPointer first;
catalogPointer second;
catalogPointer tmp;
first=head;
second=NULL;
strcpy(newNode->short_name, short_name);
strcpy(newNode->surname, surname);
newNode->amount=amount;
strcpy(newNode->description, description);
while (first!=NULL)
{ if (strcmp(surname,first->surname)>0)
second=first;
else if (strcmp(surname,first->surname)==0)
{
if (strcmp(short_name,first->short_name)>0)
second=first;
}
first=first->next;
}
if (second==NULL)
{ newNode->next=head;
head=newNode;
}
else //SEGMENTATION APPEARS WHEN IT GETS HERE!
{ tmp=second->next;
newNode->next=tmp;
first->next=newNode;
}
}更新: SegFault只有在进入InsertSort()函数的'else‘循环时才会出现。我观察到,当我尝试将它后面的名称放在列表中时,出现了分段错误。例如,如果列表中存在:
姓名:B姓氏:B金额:6描述:B姓名:D姓氏:D金额:4描述:D姓名:G姓氏:G金额:2描述:G
我输入:"x z 77 gege“有一个分段,但如果我输入”x a 77 gege“,它会正常继续……
发布于 2010-05-26 02:35:03
不能在评论中发帖,所以它是这样的:
while (first!=NULL) { //-> this loop can exit ONLY with 'first' being NULL
if (strcmp(surname,first->surname)>0)
second=first;
else if (strcmp(surname,first->surname)==0) {
if (strcmp(short_name,first->short_name)>0)
second=first;
}
first=first->next;
}
if (second==NULL) {
newNode->next=head;
head=newNode;
}
else {
tmp=second->next;
newNode->next=tmp;
first->next=newNode; // first used (but it's NULL!)
}换句话说,如果你的程序在循环中找到任何满足条件的条目并设置为'second‘,它就会崩溃。(这会触发“在”列表内部“的预期添加”)。
Ok ~没有时间等待答案:o),如果你想输入"after“the 'second‘将代码更改为:
if (second==NULL) {
newNode->next=head;
head=newNode;
}
else {
newNode->next=second->next;
second->next=newNode;
}解释(S是'second',N是'newNode',A B只是列表中的一些现有条目):
initial:
N
A -> S -> B
first assignment:
N ---\
|
v
A -> S -> B
second assignment:
N ---\
^ |
| v
A -> S B
and thus:
A-> S -> N -> B发布于 2010-05-26 00:49:11
不确定是什么导致了这个bug,但我确实看到了这个糟糕的模式:
char *short_name;
short_name = malloc(strlen(curToken) + 1);
strncpy(short_name, curToken, sizeof (short_name)); sizeof(short_name)将始终是相同的(对于32位平台,通常为4,对于64位平台,通常为8),因此此处使用的值不正确。你应该做的是:
strncpy(short_name, curToken, strlen(curToken) + 1);发布于 2010-05-26 00:51:02
使用像valgrind这样的东西来查找类似这样的问题。
https://stackoverflow.com/questions/2906600
复制相似问题