C 试图释放无效指针

C 试图释放无效指针,c,pointers,free,C,Pointers,Free,我有一本书 *** glibc detected *** [...] free(): invalid pointer: 0x0804d0d0 *** 在该功能中: ptrGBloque determinar_nodo(const char* path){ // Si es el directorio raiz, devuelve 0: if(!strcmp(path, "/")) return 0; int fd, i, nodo_anterior, aux;

我有一本书

*** glibc detected *** [...] free(): invalid pointer: 0x0804d0d0 ***
在该功能中:

ptrGBloque determinar_nodo(const char* path){
    // Si es el directorio raiz, devuelve 0:
    if(!strcmp(path, "/")) return 0;

    int fd, i, nodo_anterior, aux;
    // Super_path usado para obtener la parte superior del path, sin el nombre.
    char *super_path = (char*) malloc(strlen(path)), *nombre = (char*) malloc(strlen(path));
    char *start = nombre, *start_super_path = super_path; //Estos liberaran memoria.
    struct grasa_file_t *node, *inicio;
    unsigned char *node_name;
    strcpy(super_path, path);
    strcpy(nombre, path);
    // Obtiene y acomoda el nombre del archivo.
    if (lastchar(path, '/')) {
            nombre[strlen(nombre)-1] = '\0';
    }
    nombre = strrchr(nombre, '/');
    nombre[0] = '\0';
    nombre = &nombre[1]; // Acomoda el nombre, ya que el primer digito siempre es '/'

    // Acomoda el super_path
    if (lastchar(super_path, '/')) {
            super_path[strlen(super_path)-1] = '\0';
    }
    aux = strlen(super_path) - strlen(nombre);
    super_path[aux] = '\0';

    nodo_anterior = determinar_nodo(super_path);

    // Abrir conexion y traer directorios, guarda el bloque de inicio para luego liberar memoria
    if ((fd = open(DISC_PATH, O_RDONLY, 0)) == -1) {
            printf("ERROR");
            return -ENOENT;
    }
    node = (void*) mmap(NULL, HEADER_SIZE_B + BITMAP_SIZE_B + NODE_TABLE_SIZE_B , PROT_READ, MAP_SHARED, fd, 0);
    inicio = node;
    node = &(node[GFILEBYBLOCK + BITMAP_BLOCK_SIZE]);

    // Busca el nodo sobre el cual se encuentre el nombre.
    node_name = &(node->fname[0]);
    for (i = 0; ( (node->parent_dir_block != nodo_anterior) | (strcmp(nombre, (char*) node_name) != 0) | (node->state == 0)) &  (i < GFILEBYTABLE) ; i++ ){
            node = &(node[1]);
            node_name = &(node->fname[0]);
    }

    // Cierra conexiones y libera memoria.
    free(start);
    free(start_super_path);
    if (munmap(inicio, HEADER_SIZE_B + BITMAP_SIZE_B + NODE_TABLE_SIZE_B) == -1) printf("ERROR");
    close(fd);
    if (i >= GFILEBYTABLE) return -1;
    return (i+1);
}
由于去bug,代码中有许多多余的步骤,请尝试逐步完成它们

对我来说真正重要的是记忆问题。注意,在声明结束时,我创建了start和start_super_路径指针,只是为了不丢失函数末尾的空闲地址。 此外,似乎函数在第二次调用时中断,如果它被调用。这意味着第一个“free”正常工作,对应于调用
determinaru-nodo(“/Otra-rupetita/”)
,但它在调用
determinaru-nodo(/Otra-rupetita/内部Otra/”)时中断

在内存浏览器上,我注意到函数试图释放的地址确实是正确的,所以我真的不知道会发生什么。 无论如何,如果我释放了其中任何一个,我就得到了上面提到的错误

提前感谢您的帮助,请原谅我的英语不好。

您必须有
(char*)malloc(strlen(path)+1)
(\0终止字符+1)

否则,会出现缓冲区溢出,从而损坏堆。

相关链接:

这就是为什么您应该始终用英语编写源代码的原因。。。
determinar_nodo("/Otra Carpetita/Inside Otra/Inside Otra Otra :D/");