c-终端中的分段故障(核心转储),但Xcode中没有



因此,当我在IDE(xCode)中运行此代码时,我没有问题,一旦我尝试使用gcc编译器运行它,我就会遇到Segmentation Fault(核心转储)错误。我真的不确定哪里出了问题。该程序假定读入一个文本文件,并根据用户设置的行长度对文本进行调整。它还考虑了是否开始了新的段落,并打印出正确的文本。

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
struct listNode {  /* self-referential structure */
   char *data;
   struct listNode *nextPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *, char *);
char delete(LISTNODEPTR *, char *);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR, int);
void instructions(void);
void freeMemory(LISTNODEPTR);
int lineLength;
char filename[100];
int main(){

   FILE *filePonter = NULL;
   LISTNODEPTR startPtr = NULL;
   instructions();
   if((filePonter = fopen(filename, "r")) == NULL){
      printf("File can not be open. n");
      exit(1);
   }else{
     char *item = malloc(30*sizeof(char));
     char *str = malloc(BUFSIZ);
     char *data = malloc(30*sizeof(char));
     item = fgets(item, BUFSIZ,filePonter);
     while(item != NULL){
        while(item != NULL && strncmp(item, "rn",2)){
            strcat(str, item);
            //printf("%s", str);
            item = fgets(item, BUFSIZ,filePonter);
        }
        if(strncmp(str,"",1)){
            data = strtok(str," n");
            while(str != NULL && data != NULL){
                insert(&startPtr, data);
                data = strtok(NULL, " rn");
                //printf("%s", data);
            }
            free(data);
            str = malloc(BUFSIZ);
            printList(startPtr,lineLength );
            //freeMemory(startPtr);
            startPtr = NULL;
            printf("n");
        }
        item = fgets(item, BUFSIZ,filePonter);
    }
}


return 0;
}
/* Print the instructions */
void instructions(void)
{
  lineLength = 0;
  while(lineLength < 40 || lineLength > 100){
    printf("Please enter the number of characters per line.n");
    printf("Valid lengths are between 40 and 100. n");
    scanf("%d", &lineLength);
  }
  printf("Please enter the file name. n");
  scanf("%s", filename);

}
/* Insert a new value into the list in sorted order */
void insert(LISTNODEPTR *sPtr, char *value)
{
  LISTNODEPTR newPtr, previousPtr, currentPtr;
  newPtr = malloc(30*sizeof(LISTNODE));
  if (newPtr != NULL) {    /* is space available */
    newPtr->data = value;
    newPtr->nextPtr = NULL;
    previousPtr = NULL;
    currentPtr = *sPtr;
    while (currentPtr != NULL && value > currentPtr->data) {
        previousPtr = currentPtr;          /* walk to ...   */
        currentPtr = currentPtr->nextPtr;  /* ... next node */
    }
    if (previousPtr == NULL) {
        newPtr->nextPtr = *sPtr;
        *sPtr = newPtr;
    }
    else {
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
      }
  }
  else
    printf("%s not inserted. No memory available.n", value);
}
/* Delete a list element */
char delete(LISTNODEPTR *sPtr, char *value)
{
  LISTNODEPTR previousPtr, currentPtr, tempPtr;
  if (value == (*sPtr)->data) {
    tempPtr = *sPtr;
    *sPtr = (*sPtr)->nextPtr;  /* de-thread the node */
    free(tempPtr);             /* free the de-threaded node */
    return *value;
  }
  else {
    previousPtr = *sPtr;
    currentPtr = (*sPtr)->nextPtr;
    while (currentPtr != NULL && currentPtr->data != value) {
        previousPtr = currentPtr;          /* walk to ...   */
        currentPtr = currentPtr->nextPtr;  /* ... next node */
    }
    if (currentPtr != NULL) {
        tempPtr = currentPtr;
        previousPtr->nextPtr = currentPtr->nextPtr;
        free(tempPtr);
        return *value;
    }
  }
  return '';
 }

 /* Print the list */
 void printList(LISTNODEPTR currentPtr, int length)
{
  LISTNODEPTR end = currentPtr;
  LISTNODEPTR aPtr = currentPtr;
  LISTNODEPTR begin = currentPtr;
  while(begin->nextPtr != NULL)
  {
    int numSpace[80] = {};
    int NumWSpace = 0;
    int charLine = 0;
    int leftOverSpace = 0;
    int counter = 0;
    int finalLine = 0;
    int j = 0;
    int i = 0;
    charLine = strlen(end->data)+1;
    counter = 1;
    end = end->nextPtr;
    while(charLine+strlen(end->data)+1 < length)
    {
        charLine = charLine + strlen(end->data) + 1;
        counter = counter + 1;
        if(end->nextPtr != NULL)
            end = end->nextPtr;
        else
        {
            finalLine = 1;
        }
    }
    leftOverSpace = length - (charLine-1);
    while(leftOverSpace > 0)
    {
        if(NumWSpace < (counter - 1))
        {
            numSpace[NumWSpace] = numSpace[NumWSpace] + 1;
            leftOverSpace = leftOverSpace - 1;
            NumWSpace = NumWSpace + 1;
        }
        else
            NumWSpace = 0;
    }

    /*This loop will print out the words for the line it is on*/
    for( i = 1; i <= counter; i++)
    {
        if(i < counter)
        {
            printf("%s ", aPtr->data);
            for(j = 1; j <= numSpace[i-1]; j++)
            {
                printf(" ");
            }
        }
        else if(i == counter)
        {
            printf("%sn", aPtr->data);
        }
        if(aPtr->nextPtr != NULL)
            aPtr = aPtr->nextPtr;
        else
            break;
    }
    if(aPtr->nextPtr != NULL)
        end = aPtr;
    if(end->nextPtr == NULL)
        begin = end;
    //printf("n");
  }
}
void freeMemory(LISTNODEPTR startPtr)//Does not work
{
  LISTNODEPTR temp;
  while(startPtr->nextPtr != NULL)
  {
     temp = startPtr->nextPtr;
    free(startPtr->data);
    free(startPtr);
    startPtr = temp;
  }

}

文件中的文本

The President shall be Commander in Chief of the Army and Navy of the United States, and of the Militia of the several States, when called into 
the actual Service of the United States; he may require the Opinion, in writing, of the principal 
Officer in each of the executive Departments, upon any subject relating to the Duties of their respective Offices, 
and he shall have Power to Grant Reprieves and Pardons for Offenses against The United States, except in Cases of Impeachment.

问题就在这里:

 // Here you allocate 30 chars
 char *item = (char *)malloc(30*sizeof(char));  
 // and some lines below you read upto BUFSIZ chars
 item = fgets(item, BUFSIZ,filePonter);

BUFSIZ在我的平台上是512。

但您的代码中可能还有更多问题。例如,这是高度可疑的。那个神奇的数字30是什么?

newPtr = malloc(30*sizeof(LISTNODE));

最新更新