Namespaces
Variants

std::bad_alloc

cppreference.net에서
< cpp | memory | new
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐지)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
교체 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
메모리 관리 라이브러리
할당기
메모리 자원
초기화되지 않은 저장소 (C++20까지*)
가비지 컬렉터 지원 (C++23까지)
 
 
헤더 <new>
class bad_alloc;

std::bad_alloc에 정의됨. 저장소 할당 실패를 보고하기 위해 할당 함수들에 의해 예외로 던져지는 객체의 타입입니다.

cpp/error/exception

상속 다이어그램

멤버 함수

(생성자)
새로운 bad_alloc 객체를 생성합니다
(public 멤버 함수)
operator=
bad_alloc 객체를 대체합니다
(public 멤버 함수)
what
설명 문자열을 반환합니다
(public 멤버 함수)

std::bad_alloc:: bad_alloc

(1)
bad_alloc ( ) throw ( ) ;
(C++11 이전)
bad_alloc ( ) noexcept ;
(C++11 이후)
(C++26 이후 constexpr)
(2)
bad_alloc ( const bad_alloc & other ) throw ( ) ;
(C++11 이전)
bad_alloc ( const bad_alloc & other ) noexcept ;
(C++11 이후)
(C++26 이후 constexpr)

구현 정의된 널 종료 바이트 문자열을 가진 새로운 bad_alloc 객체를 생성합니다. 이 문자열은 what() 을 통해 접근할 수 있습니다.

1) 기본 생성자.
2) 복사 생성자. 만약 * this 와 other 모두 동적 타입이 std::bad_alloc 이라면 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. (C++11 이후)

매개변수

other - 복사할 다른 예외 객체

std::bad_alloc::operator=

bad_alloc& operator=( const bad_alloc& other ) throw();
(C++11까지)
bad_alloc& operator=( const bad_alloc& other ) noexcept;
(C++11부터)

내용을 other의 내용으로 할당합니다. 만약 *this과 other 모두 동적 타입이 std::bad_alloc라면, 할당 후 std::strcmp(what(), other.what()) == 0입니다.(C++11부터)

매개변수

other - 할당할 다른 예외 객체

반환값

* this

std::bad_alloc::what

virtual const char* what() const throw();
(C++11까지)
virtual const char* what() const noexcept;
(C++11부터)
(C++26부터 constexpr)

설명 문자열을 반환합니다.

반환값

설명 정보를 담은 구현 정의 널 종료 문자열에 대한 포인터입니다. 이 문자열은 std::wstring 으로 변환 및 표시하기에 적합합니다. 이 포인터는 적어도 해당 예외 객체가 파괴되거나 예외 객체의 비상수 멤버 함수(예: 복사 할당 연산자)가 호출되기 전까지는 유효함이 보장됩니다.

상수 평가 중에는 반환된 문자열이 일반 리터럴 인코딩으로 인코딩됩니다.

(C++26 이후)

참고

구현체는 what() 을 재정의할 수 있지만 필수는 아닙니다.

std::exception으로부터 std:: exception 상속됨

멤버 함수

[virtual]
예외 객체를 파괴함
( std::exception 의 virtual public 멤버 함수)
[virtual]
설명 문자열을 반환함
( std::exception 의 virtual public 멤버 함수)

참고 사항

기능 테스트 매크로 값 표준 기능
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr 예외 타입

예제

#include <iostream>
#include <new>
int main()
{
    try
    {
        while (true)
        {
            new int[100000000ul];
        }
    }
    catch (const std::bad_alloc& e)
    {
        std::cout << "Allocation failed: " << e.what() << '\n';
    }
}

가능한 출력:

Allocation failed: std::bad_alloc

참고 항목

할당 함수
(함수)