在C中重新定义空闲内存函数

发布于 2024-10-11 17:13:10 字数 1120 浏览 9 评论 0原文

我正在用 C 重新定义内存函数,我想知道这个想法是否可以作为 free() 函数的实现:

    typedef struct _mem_dictionary
    {
        void *addr;
        size_t size;
    } mem_dictionary;

    mem_dictionary *dictionary = NULL; //array of memory dictionaries
    int dictionary_ct = 0;         //dictionary struct counter

void *malloc(size_t size)
   {
    void *return_ptr = (void *) sbrk(size); 

    if (dictionary == NULL) 
        dictionary = (void *) sbrk(1024 * sizeof(mem_dictionary));  




    dictionary[dictionary_ct].addr = return_ptr; 
    dictionary[dictionary_ct].size = size;      
    dictionary_ct++;                 
    printf("malloc(): %p assigned memory\n",return_ptr); 
    return return_ptr;               

    }

    void free(void *ptr)
    {


    size_t i;
    int flag = 0;

     for(i = 0; i < dictionary_ct ; i++){

        if(dictionary[i].addr == ptr){
            dictionary[i].addr=NULL;
            dictionary[i].size = 0;
            flag = 1;
            break;
            }
        }

        if(!flag){
            printf("Remember to free!\n");
        }


    }

提前致谢!

I'm redefining memory functions in C and I wonder if this idea could work as implementation for the free() function:

    typedef struct _mem_dictionary
    {
        void *addr;
        size_t size;
    } mem_dictionary;

    mem_dictionary *dictionary = NULL; //array of memory dictionaries
    int dictionary_ct = 0;         //dictionary struct counter

void *malloc(size_t size)
   {
    void *return_ptr = (void *) sbrk(size); 

    if (dictionary == NULL) 
        dictionary = (void *) sbrk(1024 * sizeof(mem_dictionary));  




    dictionary[dictionary_ct].addr = return_ptr; 
    dictionary[dictionary_ct].size = size;      
    dictionary_ct++;                 
    printf("malloc(): %p assigned memory\n",return_ptr); 
    return return_ptr;               

    }

    void free(void *ptr)
    {


    size_t i;
    int flag = 0;

     for(i = 0; i < dictionary_ct ; i++){

        if(dictionary[i].addr == ptr){
            dictionary[i].addr=NULL;
            dictionary[i].size = 0;
            flag = 1;
            break;
            }
        }

        if(!flag){
            printf("Remember to free!\n");
        }


    }

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-10-18 17:13:10

不,不会的。在这样的调用之后,您“释放”的地址实际上会丢失。您如何知道特定的内存块再次可用于分配?

这方面有很多研究,这里有一些概述 - 快速内存分配在多布斯博士那里。

编辑0:

您对 sbrk 的看法是错误的(2) - 它不是“更好的 malloc”,您不能这样使用它。该系统调用修改进程数据段的结尾

No, it will not. The address you are "freeing" is effectively lost after such a call. How would you ever know that the particular chunk of memory is again available for allocation?

There has been a lot of research in this area, here is some overview - Fast Memory Allocation in Dr. Dobbs.

Edit 0:

You are wrong about sbrk(2) - it's not a "better malloc" and you cannot use it as such. That system call modifies end of process data segment.

最偏执的依靠 2024-10-18 17:13:10

几件事:

  1. 您在哪里为字典分配内存?
  2. 如何分配dictionary->addr指向的内存?如果没有 malloc 的代码,就看不到您的 free 是否可以工作。
  3. 除非在您的 malloc 函数中,您要遍历进程可用的每个内存地址来检查它是否未被您的 字典 使用,而仅仅是赋值 Dictionary[i].addr=NULL 不会“释放”内存,并且绝对不会保留它以供重用。

顺便说一句,当用户在据称未分配的指针上调用 free 时,您的 free 版本中的 printf 函数将打印 Remember to free! , 正确的?那为什么要“记得免费”呢?

编辑:

因此,使用 malloc 函数,不,您的 free 不会释放内存。首先,您将丢失内存的地址,因此每次调用此 malloc 时,您实际上都会进一步推动进程中断,并且永远不会重用已释放的内存位置。解决此问题的一种方法是以某种方式跟踪您已“释放”的位置,以便下次调用 malloc 时,您可以检查是否已分配给进程足够的可用内存,并且然后重用这些位置。另外,请记住,sbrkbrk 的包装器,这是一个昂贵的系统调用,您应该优化您的 malloc,以便占用大量内存使用 sbrk 从操作系统请求,然后跟踪您正在使用哪个部分以及哪个部分可用。

Few things:

  1. Where do you allocate the memory for the dictionary?
  2. How do you allocate the memory that dictionary->addr is pointing at? Without having the code for your malloc it is not visible if your free would work.
  3. Unless in your malloc function you're going through each and every memory address available to the process to check if it is not used by your dictionary, merely the assignment dictionary[i].addr=NULL would not "free" the memory, and definitely not keep it for reuse.

BTW, the printf function in your version of free would print Remember to free! when the user calls free on a pointer that is supposedly not allocated, right? Then why "remember to free"?

Edit:

So with that malloc function, no, your free does not free the memory. First of all, you're losing the address of the memory, so every time you call this malloc you're actually pushing the process break a little further, and never reuse freed memory locations. One way to solve this is to somehow keep track of locations that you have "freed" so that next time that malloc is called, you can check if you have enough available memory already allocated to the process, and then reuse those locations. Also, remember that sbrk is a wrapper around brk which is an expensive system call, you should optimize your malloc so that a big chunk of memory is requested from OS using sbrk and then just keep track of which part you're using, and which part is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文