malloc,重铸和免费 [英] malloc, recasting and free

查看:57
本文介绍了malloc,重铸和免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些结构可以存储不同种类的列表:typedef int db_int;typedef char db_string [DB_STRING_LEN];

I have some structures to store different kinds of lists: typedef int db_int; typedef char db_string[DB_STRING_LEN];

struct List_db_int {
    struct List_db_int *next;
    struct List_db_int *prev;
    db_int v;
};
struct List_db_string {
    struct List_db_string *next;
    struct List_db_string *prev;
    db_string v;
};
struct List_db_void {
    struct List_db_void *next;
    struct List_db_void *prev;
};

我也有一个可以存储任何列表指针的联合体:

I also have an union which can store any of this list pointers:

union Uni_list {
    struct List_db_int *db_type_int;
    struct List_db_string *db_type_string;
    struct List_db_void *db_type_void;
};

我想创建一个函数,该函数将从列表中删除某些元素,但是我希望它与列表类型无关,所以我提供了以下解决方案:

I want to create a function which will delete some elements from lists but I want it to be list type agnostic so I come with following solution:

    /*data is an array of pointers to begin of lists*/
    union Uni_list *data;
    struct List_db_void *last; 
    for (i = 0; i < ELEMENTS_; i++) {
        last = data[i].db_type_void->next;
        for (j = 0; j < HOW_MANY_ELEMENTS_REMOVE; j++) {
            free_list(last, t->cols[i]/*type of column*/);
            last = last->next;
        }
        data[i].db_type_void->next = last;
    }

这是free_list函数:无效

Here is the free_list function: void

free_list(struct List_db_void *elm, enum db_type type) {
    switch(type) {
        case db_type_int:
            free((struct List_db_int*) elm);
            break;
        case db_type_string:
            free((struct List_db_string*) elm);
            break;
        default:
            /*Should not reach*/
            return ;
            break;
    }
}

但是它不能正常工作,当我尝试读取修改后的列表时,我最终得到:

But it does not work correctly and when I try to read modified lists, I end up with:

*** Error in `./ppbase': free(): invalid pointer: 0x00007f84aa47f678 ***                                                       
======= Backtrace: =========                                                                                                   
/usr/lib/libc.so.6(+0x72ecf)[0x7f84aa14cecf]                                                                                   
/usr/lib/libc.so.6(+0x7869e)[0x7f84aa15269e]                                                                                   
/usr/lib/libc.so.6(+0x79377)[0x7f84aa153377]                                                                                   
./ppbase[0x402001]                                                                                                             
./ppbase[0x40217e]                                                                                                             
./ppbase[0x40273d]                                                                                                             
/usr/lib/libc.so.6(__libc_start_main+0xf5)[0x7f84aa0fbbc5]                                                                     
./ppbase[0x4009d9]                                                                                                             
======= Memory map: ========                                                                                                   
00400000-00404000 r-xp 00000000 08:04 10883382                           /home/hafron/dev/ppbase/ppbase                        
00603000-00604000 rw-p 00003000 08:04 10883382                           /home/hafron/dev/ppbase/ppbase                        
01797000-017b8000 rw-p 00000000 00:00 0                                  [heap]                                                
7f84a9ec4000-7f84a9ed9000 r-xp 00000000 08:03 49858                      /usr/lib/libgcc_s.so.1                                
7f84a9ed9000-7f84aa0d9000 ---p 00015000 08:03 49858                      /usr/lib/libgcc_s.so.1                                
7f84aa0d9000-7f84aa0da000 rw-p 00015000 08:03 49858                      /usr/lib/libgcc_s.so.1                                
7f84aa0da000-7f84aa27c000 r-xp 00000000 08:03 9136                       /usr/lib/libc-2.18.so                                 
7f84aa27c000-7f84aa47b000 ---p 001a2000 08:03 9136                       /usr/lib/libc-2.18.so                                 
7f84aa47b000-7f84aa47f000 r--p 001a1000 08:03 9136                       /usr/lib/libc-2.18.so                                 
7f84aa47f000-7f84aa481000 rw-p 001a5000 08:03 9136                       /usr/lib/libc-2.18.so                                 
7f84aa481000-7f84aa485000 rw-p 00000000 00:00 0                                                                                
7f84aa485000-7f84aa4a5000 r-xp 00000000 08:03 7209                       /usr/lib/ld-2.18.so                                   
7f84aa672000-7f84aa675000 rw-p 00000000 00:00 0                                                                                
7f84aa6a1000-7f84aa6a4000 rw-p 00000000 00:00 0                                                                                
7f84aa6a4000-7f84aa6a5000 r--p 0001f000 08:03 7209                       /usr/lib/ld-2.18.so                                   
7f84aa6a5000-7f84aa6a6000 rw-p 00020000 08:03 7209                       /usr/lib/ld-2.18.so                                   
7f84aa6a6000-7f84aa6a7000 rw-p 00000000 00:00 0                                                                                
7fff249c8000-7fff249e9000 rw-p 00000000 00:00 0                          [stack]                                               
7fff249fe000-7fff24a00000 r-xp 00000000 00:00 0                          [vdso]                                                
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]                                            
zsh: abort (core dumped)  ./ppbase < tests/filter       

如何正确使用此代码中的自由功能?我可以将任何列表投射到db_type_void-> next(第二个示例中的第5行)上吗?

How should I use free function in this code correctly? Can I safly cast any of the List into: db_type_void->next (line 5 in second example)?

推荐答案

此功能:

free_list(struct List_db_void *elm, enum db_type type) {
    switch(type) {
        case db_type_int:
            free((struct List_db_int*) elm);
            break;
        case db_type_string:
            free((struct List_db_string*) elm);
            break;
        default:
            /*Should not reach*/
            return ;
            break;
    }
}

与(与您的评论所建议的那样,我们从未经过 default 分支一样)完全相同

is exactly the same as (considering that we never go through the default branch, as your comment suggests)

free_list(struct List_db_void *elm, enum db_type type) {
    free(elm);
}

指针类型转换适用于类型系统,并且具有 no 动态语义.可以说,它们在编译后消失了.

Pointer casts are for the type system and have no dynamic semantics. They disappear after compiling, so to speak.

话虽这么说,我正在看看是什么导致了段错误.

This being said I'm taking a look to see what causes the segfault.

编辑:可能是这样!

for (j = 0; j < HOW_MANY_ELEMENTS_REMOVE; j++) {
    free_list(last, t->cols[i]/*type of column*/);
    last = last->next;
}

释放后,您正在使用 last .做类似的事情

You're using last after freeing it. Do something like

for (j = 0; j < HOW_MANY_ELEMENTS_REMOVE; j++) {
    struct List_db_void *temp = last->next; 

    free_list(last, t->cols[i]/*type of column*/);
    last = temp;
}

这篇关于malloc,重铸和免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆