munmap_chunk C 中的错误

munmap_chunk error in C

我一直在试验动态内存分配,我在 C 中遇到了 munmap_chunk 错误。 这是我的代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void get_input(char **input) {
  *input = (char *) malloc (100);
  *input = "hello world";
}

int main() {
  char *input;
  get_input(&input);
  puts(input);
  free(input);
  return 0;
}

这是执行程序时 valgrind 显示的内容。

==4116== Memcheck, a memory error detector
==4116== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4116== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4116== Command: ./a.out
==4116== 
hello world
==4116== Invalid free() / delete / delete[] / realloc()
==4116==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4116==    by 0x400615: main (in /home/mark/Documents/CS32/a.out)
==4116==  Address 0x4006a4 is not stack'd, malloc'd or (recently) free'd
==4116== 
==4116== 
==4116== HEAP SUMMARY:
==4116==     in use at exit: 100 bytes in 1 blocks
==4116==   total heap usage: 1 allocs, 1 frees, 100 bytes allocated
==4116== 
==4116== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4116==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4116==    by 0x4005D2: get_input (in /home/mark/Documents/CS32/a.out)
==4116==    by 0x4005FD: main (in /home/mark/Documents/CS32/a.out)
==4116== 
==4116== LEAK SUMMARY:
==4116==    definitely lost: 100 bytes in 1 blocks
==4116==    indirectly lost: 0 bytes in 0 blocks
==4116==      possibly lost: 0 bytes in 0 blocks
==4116==    still reachable: 0 bytes in 0 blocks
==4116==         suppressed: 0 bytes in 0 blocks
==4116== 
==4116== For counts of detected and suppressed errors, rerun with: -v
==4116== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

为什么 free() 函数会这样?另外,从 valgrind 日志来看,为什么我的 char *input 变量没有被 malloc() 分配?

你可以这样赋值:

*input = "hello world"; /* Not modifiable */

但是,你不需要用 malloc 保留 space,"hello world" 有他自己的地址(在某些 "read only" 段)和任何尝试修改字符串会导致未定义的行为,因此您不能 free 它。

如果你想要一个可修改的字符串:

void get_input(char **input) {
    char str[] = "hello world";

    *input = malloc(sizeof(str)); /* Don't cast malloc */
    if (*input == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    strncpy(*input, str, sizeof(str));
}

void get_input(char **input) {
    *input = strdup("hello world");
}

请注意,strdup 不是标准的,但在许多实现中都可用。

问题出在 malloc 之后的这一行。 (顺便说一句,不要使用 malloc return。)

*input = "hello world";

发生了什么? "hello world" 告诉编译器在 space 中创建一些数据,我们称之为数据段,并插入表示字符串 "hello world" 的字节,包括在那里终止 '[=16=]' 。然后生成代码,将指向该字符串的指针插入 input 指向的位置。这意味着您使前一行代码无用。想象一下这两个作业一个接一个x = 1; x = 2;。这是你刚刚做的,但是 x*input。 然后故事继续,你从 get_input() return 最后得到这个指向 main 函数范围内变量 input 中的字符串的指针。然后将其输入 free() 调用。但它不是由 malloc() 编辑的指针 return。你输了。

如果您想在输入中输入 "hello world",您可以执行

void get_input(char **input) {
  *input = (char *) malloc (100);
  strcpy(*input, "hello world");
}

int main() {
  char *input;
  get_input(&input);
  puts(input);
  free(input);
  return 0;
}

void get_input(char **input) {
  *input = "hello world";
}

int main() {
  char *input;
  get_input(&input);
  puts(input);
  return 0;
}

但现在你应该小心处理由 input 指向的数据,因为它指向你无法修改的内存部分,free()realloc()