c语言 - 为什么free()不起作用?



每次我将输入存储在字符*中分配的空间上方时,我都会收到free()的错误。这是错误:

*** Error in ./input': free(): invalid next size (fast): 0x09713008 ***

当我删除free()时,即使我输入的大小超过分配的大小,程序也能完美运行。为什么会这样?我该如何预防?这是我的代码供参考:

int main(void){
  float x; // used to store the float the user entered.
  char c; // character used to check if the user entered a character after the float
  int loop=0;
  char * usr_input = malloc(50); //allocates memory to store the string from the stdin
  // loops until the user enters a float and only a float
  do{
    //gets the input string from stdin
    scanf("%s",usr_input);
    if(usr_input==NULL)
        printf("You've entered a large number that isnt supported. Please use at most 5 digitsn");
    // parses the input received and checks if the user entered a float and only a float.
    // breaks the loop if they did
    else if(sscanf(usr_input,"%f %c",&x,&c) == 1){
        if (x!=inf)
            loop=1;
        else
            printf("Input was too large. Try again");
    }
    // tells the user they entered invalid input. Loop value doesnt change so function loops again
    else{
        printf("Invalid input. Try againn");
    }
  }
  while(loop==0); // condition for the loop
  free(usr_input);//crashes here
  return x; // returns the valid float that was entered
}

当我删除free()时,即使我输入的大小超过分配的大小,程序也能完美运行。

输入超过分配的大小称为未定义行为。这是一个错误,尽管您的程序可能看起来"运行良好"。

未定义行为的主要问题是程序不会快速失败。从本质上讲,对未定义行为的惩罚会延迟到将来的某个时间 - 例如,当您再次分配或释放时。

malloc在分配的块中存储一些特殊信息,以便free运行。"无效的下一个大小"错误通常意味着您的代码已经覆盖了一些隐藏的数据块。

要解决此问题,您需要更改代码,使其永远不会写入超过分配的长度。如果您在精确检测需要更改的点时遇到问题,请考虑使用 valgrind 或其他内存分析器。

若要防止scanf写入分配的大小,请使用格式字符串中的大小:

scanf("%49s",usr_input); // Pass 49 for the length, because you have 50 bytes, and you need 1 byte for ''

程序运行完美,即使我输入的超过 分配的大小。

不,它不能完美运行。实际上,您得到的错误是由超出分配缓冲区的边界写入引起的。它是缓冲区溢出并引入未定义的行为。您的程序可能会工作或立即崩溃,尽管在大多数情况下,它会导致以后出现问题,这些问题可能看起来完全无关,因此很难识别和纠正。

确保分配的缓冲区足够大,以免覆盖它。

另一方面,在堆上分配那个小缓冲区是没有意义的。它可以是堆栈上的静态缓冲区,您可以避免内存分配和释放问题。

最新更新