Can I force std::vector to leave a memory leak?

前端 未结 7 940
日久生厌
日久生厌 2021-01-18 00:45

Can I force std::vector to not deallocate its memory after the vector goes out of scope?

For example, if I have



        
7条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 01:27

    Not sure but, yes.

    You can create a custum allocator who do nothing when deallocate => leak

    Or may be you can jsut create your vectoron the heap so it will leak anyway.

    int* foo() {
        std::vector* v = new std::vector(10,1); 
        return &((*v)[0]);
        // no delete
    }
    
    int main()
    {
        int* bar = foo();
        std::cout << bar[5] << std::endl;
    }
    

提交回复
热议问题