티스토리 뷰

카테고리 없음

bad_alloc 관련

newpolaris 2020. 6. 10. 07:50

참고: MFC에서는 CMemoryException 으로 예외를 던지는데 catch(CMemoryException* e) 로 받아야 한다

https://m.blog.naver.com/PostView.nhn?blogId=devmachine&logNo=207239711&proxyReferer=https:%2F%2Fwww.google.com%2F

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1404r1.html

#include <iostream>
#include <vector>
#include <string>

int main()
{
    typedef int64_t type;

    try {
        std::vector<char> v (std::numeric_limits<type>::max()); // huge allocation
        std::cout << "OK" << std::endl;
    }
    catch (std::exception const& e) // bad_alloc handled as any other exception
    {
        std::cout << e.what() << std::endl;
        std::cout << typeid(e).name() << std::endl;
    }
}

https://en.cppreference.com/w/cpp/error/exception

http://faq.hostway.co.kr/Linux_ETC/7179

newpolaris@MacBook-Pro ~ % ulimit -a
-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       2784
-n: file descriptors                256

테스트

#include <iostream>
#include <vector>
#include <string>
#include <sys/types.h>
#include <sys/resource.h>
#include <unistd.h>

int main()
{
    struct rlimit rlim;

    getrlimit(RLIMIT_AS, &rlim);
    std::cout << rlim.rlim_cur << std::endl;
    std::cout << rlim.rlim_max << std::endl;

    rlim.rlim_cur = 1024*1024;
    rlim.rlim_max = 1024*1024;
    setrlimit(RLIMIT_AS, &rlim);

    getrlimit(RLIMIT_AS, &rlim);
    std::cout << rlim.rlim_cur << std::endl;
    std::cout << rlim.rlim_max << std::endl;

    std::cout << "INFI: " << RLIM_INFINITY << std::endl;

    try {
        std::vector<char> v (1024 * 1024 * 1024); // huge allocation
        v[0] = 0;
        std::cout << "OK" << std::endl;
    }
    catch (std::exception const& e) // bad_alloc handled as any other exception
    {
        std::cout << e.what() << std::endl;
        std::cout << typeid(e).name() << std::endl;
    }
}

int64_t

a.out(57022,0x11975cdc0) malloc: can't allocate region
*** mach_vm_map(size=9223372036854775808) failed (error code=3)
a.out(57022,0x11975cdc0) malloc: *** set a breakpoint in malloc_error_break to debug
std::bad_alloc
St9bad_alloc

size_t

vector
St12length_error
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크