Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
终端中出现分段故障(堆芯转储),但Xcode中没有_C_Segmentation Fault - Fatal编程技术网

终端中出现分段故障(堆芯转储),但Xcode中没有

终端中出现分段故障(堆芯转储),但Xcode中没有,c,segmentation-fault,C,Segmentation Fault,所以,当我在我的IDExCode中运行这段代码时,我没有任何问题,一旦我尝试使用gcc编译器运行它,我就会得到一个分段错误核心转储错误。我真的不知道我会错在哪里。假设程序读取文本文件,并根据用户设置的行长度对文本进行对齐。它还考虑到新段落是否已开始,并以正确的理由打印文本 #include <stdio.h> #include <stdlib.h> #include <string.h> struct listNode { /* self-refer

所以,当我在我的IDExCode中运行这段代码时,我没有任何问题,一旦我尝试使用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;
  }


}
问题在于:

 // 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是什么

问题在于:

 // 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是什么


使用gdb运行它,Gnu调试器。使用gcc版本4.6.3在我的64位Ubuntu12.04上编译。我没有得到任何分割错误。!看起来像是特定于环境的东西。正如@MichaelWalz所建议的,使用gdb.libe未定义的行为未初始化的变量,越界问题,…strcatstr,item;:首先,需要*str=0;在main末尾:item=fgetsitem、BUFSIZ、filePonter;此时项为空。这一行应该简单地删除。在缓冲区为NULL的情况下调用FGETSWIST充其量是无用的,充其量是未定义的行为。使用gdb(Gnu调试器)运行它。使用gcc版本4.6.3在我的64位Ubuntu12.04上编译。我没有得到任何分割错误。!看起来像是特定于环境的东西。正如@MichaelWalz所建议的,使用gdb.libe未定义的行为未初始化的变量,越界问题,…strcatstr,item;:首先,需要*str=0;在main末尾:item=fgetsitem、BUFSIZ、filePonter;此时项为空。这一行应该简单地删除。在缓冲区为NULL的情况下调用fgetswi充其量是无用的,充其量是未定义的行为。
newPtr = malloc(30*sizeof(LISTNODE));