C++ How to allocate memory dynamically on stack?

后端 未结 7 1035
南旧
南旧 2020-12-04 11:07

Is there a way to allocate memory on stack instead of heap? I can\'t find a good book on this, anyone here got an idea?

7条回答
  •  离开以前
    2020-12-04 11:55

    You could use the BDE C++ library, e.g.

    const int BUFFER_SIZE = 1024;
    char      buffer[BUFFER_SIZE];
    
    bdlma::BufferedSequentialAllocator allocator(buffer, BUFFER_SIZE);
    bsl::vector                   dataVector(&allocator);
    
    dataVector.resize(50);
    

    BDE supplies comprehensive allocator options along with collections like bsl::vector that can use polymorphic allocators without changing the type of the container.

    You might also consider:

    • https://github.com/facebook/folly/blob/master/folly/docs/small_vector.md
    • http://www.boost.org/doc/libs/1_55_0/doc/html/container/non_standard_containers.html#container.non_standard_containers.static_vector
    • http://llvm.org/docs/doxygen/html/classllvm_1_1SmallVector.html

提交回复
热议问题