How do I force a program to appear to run out of memory?

后端 未结 9 920
旧巷少年郎
旧巷少年郎 2020-12-24 07:39

I have a C/C++ program that might be hanging when it runs out of memory. We discovered this by running many copies at the same time. I want to debug the program without co

9条回答
  •  轮回少年
    2020-12-24 07:57

    One way is to write a wrapper around malloc().

    static unsigned int requested =0;
    
    void* my_malloc(size_tamount){
    
       if (requested + amount < LIMIT){
           requested+=amount;
           return malloc(amount);
       }
    
       return NULL
    }
    

    Your could use a #define to overload your malloc.

    As GMan states, you could overload new / delete operators as well (for the C++ case).

    Not sure if that's the best way, or what you are looking for

提交回复
热议问题