Home  >  Q&A  >  body text

c++ - 关于STL的alloc

    template<int inst>
    void (*__malloc_alloc_template<inst>::set_malloc_handler(void(*f)()))()
    {
        void(*old)() = __malloc_alloc_oom_handler;
        __malloc_alloc_oom_handler = f;
        return (old);
    }

为什么是返回old?而不是返回__malloc_alloc_oom_handler ?

void (*__malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = 0;

在这里把__malloc_alloc_oom_handler设置为了0,那么在上面那个函数old的值不也为0吗?那么我调用这个返回的old就会出错呀,为什么这么做呢?

高洛峰高洛峰2713 days ago497

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 13:05:20

    Returning old means returning the previous alloc_handler,

    __malloc_alloc_oom_handler = f;
    这里都已经设置老的hanlder为新的f了,你再返回它有意义吗,好比你传入了一个参数,结果你这个函数的返回值还是还是这个参数,没有任何意义的。

    reply
    0
  • Cancelreply