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

时间:2016-03-22 09:49:12

标签: c segmentation-fault

因此,当我在我的IDE(xCode)中运行此代码时,我没有任何问题,一旦我尝试使用gcc编译器运行它,我就会收到分段错误(核心转储)错误。不确定我哪里错了。该程序假定读取文本文件并根据用户设置的行长度证明文本的合理性。它还会考虑是否启动了新段落并使用正确的理由打印出文本。

 #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, "\r\n",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, " \r\n");
                //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 '\0';
 }


 /* 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("%s\n", 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.

1 个答案:

答案 0 :(得分:4)

问题在于:

 // 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));