C 编程语言:项目错误 - *** `./myText' 中的错误:free():无效指针:0xbffebb58 ***

C Programming Language: Project error - *** Error in `./myText': free(): invalid pointer: 0xbffebb58 ***

我最近开始学习 C,目前我正在开发一个程序,该程序接收用户的文本输入并打印文本,以便每行恰好包含 60 个字符。在练习中,我被要求为这个程序实现 2 个数据结构:缓冲区和链表。

这是我想出的:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 60

struct Node{
char data[SIZE + 1];
struct Node *next;
}; /* Node representation */

void readText(int , void *); /* function that reads the text */
void printText(int , void * , int); /* function that prints the text */
void addNode(struct Node**); /* function that adds a node to the previous node in the linked-list */

int main()
{
  int choice;
  printf("Hello! This program receives a text input and prints it so that each line will contain the 
  length of 60 characters\n");

  printf("In order to start, please, enter the type of data structure that you wish to implement (0 
  for Buffer, 1 for Linked-List):\n");

  scanf("%d", &choice);

  if(choice == 0) /* buffer choice */
  { 
    char *bufferPointer;
    bufferPointer = (char *)calloc(SIZE + 1, sizeof(char));
    readText(0 , &bufferPointer);  /* calling the read-text function to implement buffer */
  }

  else if(choice == 1) /* Linked-list choice */
  {
    struct Node *nodePointer; 
    nodePointer = (struct Node*)calloc(SIZE + 1, sizeof(struct Node));
    nodePointer->next = NULL;
    readText(1 , &nodePointer); /* calling the read-text function to implement linked list */
  }

  else /* means the user entered wrong type */
  {
    printf("Error! you have entered an invalid value!\n");
  }

    return 0;
}



void readText(int choice , void *pointer)
{
  int c;
  int i;
  i = 0;

   if(choice == 0)
   {
      char *bufPointer;
      bufPointer = (char *)pointer;

      printf("Enter the text you want to write.\nEnd, by entering (CTRL + D) -> Linux , (CTRL + Z) -> Windows\n");

      while((c = getchar()) != EOF)
      {
        if(i == SIZE)
        {
          bufPointer = (char *)realloc(bufPointer, (SIZE + 1) *sizeof(char));
          i = 0;
        }

        if(bufPointer == NULL)
        {
          printf("Sorry! The memory allocation has failed!\n");
          exit(0);
        }

          bufPointer[i] = c;
          i++;
      }

    printText(0 , &bufPointer, i);
    free(bufPointer);
   }

  if(choice == 1)
  {
    struct Node *nPointer; 
    nPointer = (struct Node*) pointer;

    printf("Enter the text you want to write.\nEnd, by entering (CTRL + D) -> Linux , (CTRL + Z) -> Windows\n");

    while((c = getchar()) != EOF)
    {
       if(i == SIZE)
       {
         addNode(&nPointer);
         i = 0;
       }

       if(nPointer == NULL)
       {
         printf("Sorry! The memory allocation has failed!\n");
         exit(0);
       }

     nPointer->data[i] = c;
     i++;
    }

   printText(1 , &nPointer , i);
   free(nPointer);
  }
}

void addNode(struct Node** node)
{
  struct Node *newNode;
  newNode = calloc(SIZE + 1 , sizeof(struct Node));

  if(newNode == NULL)
  {
    printf("Sorry! The memory allocation has failed!\n");
    exit(0);
  }

    newNode->next = (*node);
    (*node) = newNode;
}


void printText(int choice , void *pointer , int length)
{
 int j;

if(choice == 0)
{
   char *bufPointer;
   bufPointer = (char *) pointer;

   for(j = 0; j < length; j++)
   {
     if((j % SIZE == 0) && (j != 0))
     {
       printf("\n");
     }

    putchar(bufPointer[j]);
   }
} 

if(choice == 1)
 {
   struct Node *head;
   head = (struct Node*) pointer;

   while(head != NULL)
   {
     for(j = 0; j < length; j++)
     {
       putchar(head->data[j]);
     }

     if(length >= SIZE)
     {
       printf("\n");
     }

    head = head->next;
   }

  }
}

(不好意思抱歉!!)

当我编译它时,它没有显示任何编译错误。但是,每当我 运行 它时,它都会给我以下错误:

user@ubuntu:~/Desktop/Maman12$ make
gcc -g -ansi -Wall -pedantic myText.c -o myText
user@ubuntu:~/Desktop/Maman12$ ./myText
Hello! This program receives a text input and prints it so that each line will contain the length of 
60 characters
In order to start, please, enter the type of data structure that you wish to implement (0 for Buffer, 
1 for Linked-List):
0
Enter the text you want to write.
End, by entering (CTRL + D) -> Linux , (CTRL + Z) -> Windows
asfasf
*** Error in `./myText': free(): invalid pointer: 0xbffebb58 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb75d6377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb75dc2f7]
/lib/i386-linux-gnu/libc.so.6(+0x6dc31)[0xb75dcc31]
./myText[0x804875d]
./myText[0x8048632]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb7587637]
./myText[0x80484e1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:02 367783     /home/user/Desktop/Maman12/myText
08049000-0804a000 r--p 00000000 08:02 367783     /home/user/Desktop/Maman12/myText
0804a000-0804b000 rw-p 00001000 08:02 367783     /home/user/Desktop/Maman12/myText
09a32000-09a53000 rw-p 00000000 00:00 0          [heap]
b7400000-b7421000 rw-p 00000000 00:00 0 
b7421000-b7500000 ---p 00000000 00:00 0 
b753c000-b7558000 r-xp 00000000 08:02 931606     /lib/i386-linux-gnu/libgcc_s.so.1
b7558000-b7559000 rw-p 0001b000 08:02 931606     /lib/i386-linux-gnu/libgcc_s.so.1
b756f000-b771f000 r-xp 00000000 08:02 933356     /lib/i386-linux-gnu/libc-2.23.so
b771f000-b7721000 r--p 001af000 08:02 933356     /lib/i386-linux-gnu/libc-2.23.so
b7721000-b7722000 rw-p 001b1000 08:02 933356     /lib/i386-linux-gnu/libc-2.23.so
b7722000-b7725000 rw-p 00000000 00:00 0 
b773a000-b773d000 rw-p 00000000 00:00 0 
b773d000-b773f000 r--p 00000000 00:00 0          [vvar]
b773f000-b7740000 r-xp 00000000 00:00 0          [vdso]
b7740000-b7762000 r-xp 00000000 08:02 931813     /lib/i386-linux-gnu/ld-2.23.so
b7762000-b7763000 rw-p 00000000 00:00 0 
b7763000-b7764000 r--p 00022000 08:02 931813     /lib/i386-linux-gnu/ld-2.23.so
b7764000-b7765000 rw-p 00023000 08:02 931813     /lib/i386-linux-gnu/ld-2.23.so
bffcd000-bffee000 rw-p 00000000 00:00 0          [stack]
X��Aborted (core dumped)

有办法解决吗?我知道是连接到free函数的,但是不明白怎么回事..

提前感谢您的帮助!

看起来好像您实际上是在指针本身而不是它们指向的内存上调用 free。发生这种情况是因为当您将指针的地址传递给函数时,您使用地址运算符 (&) 获取指针的地址,而不是传递指针存储的值。这将导致未定义的行为,因为指针可能存储在堆栈中而不是 freed。这种未定义的行为表现为错误消息和随后的程序中止。我建议你直接传递指针,看看是否有区别。

编辑:我忘了提到函数的指针参数是char */struct Node *而不是void *,你会得到一个编译器警告,指出传递的指针有与函数预期不同的间接级别。