Chad Brown
Chad Brown

Reputation: 41

Segmentation Fault (core dump) in terminal but no in Xcode

So when i run this code in my IDE(xCode) I have no issues, once i try to run it with gcc compiler I get a Segmentation Fault (core dump) error. not really sure where I am going wrong. The program is suppose to read in a text file and justify the text according to the users set line length. it also takes into account if a new paragraph was started and prints the text out with the correct justification.

 #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;
  }


}

text that is in the file

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.

Upvotes: 1

Views: 470

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50774

The Problem is here:

 // 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 is 512 on my platform.

But there are probably more issues in your code. For example this is highly suspicious. Whats that magic number 30?

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

Upvotes: 4

Related Questions