Improvements for this C++ stack allocator?

前端 未结 4 2030
广开言路
广开言路 2020-12-31 21:42

Any suggestions for my stack based allocator? (Except for suggestions to use a class with private/public members)

struct Heap
{
    void* heap_start;
    voi         


        
4条回答
  •  天命终不由人
    2020-12-31 22:26

    Two obvious problems:

    1/ You don't have a deallocate().

    2/ A deallocate() will be very hard to write with your current strategy unless you're always going to deallocate in the exact reverse order of allocating. You'll need to cater for the case where a client wants to deallocate memory in the middle of your used section.

    Of course, if you do deallocate in reverse order, (2) is not a problem. And if you never free memory at all, (1) is also not a problem.

    It depends on what you want it to do.

提交回复
热议问题