首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C编程语言:项目错误-* `./myText':free():无效指针: 0xbffebb58 *

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

Stack Overflow用户
提问于 2021-05-06 01:54:45
回答 1查看 71关注 0票数 1

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

下面是我的想法:

代码语言:javascript
复制
#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;
   }

  }
}

(抱歉,这是个错误的建议!!)

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

代码语言:javascript
复制
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)

有没有办法解决这个问题?我知道它连接到了免费功能,但我不明白它有什么问题。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-06 03:54:24

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67406419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档