Namespaces
Variants

std:: bad_alloc

From cppreference.net
< cpp ‎ | memory ‎ | new
Utilities library
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더에 정의됨 <new>
class bad_alloc : public std:: exception

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

cpp/error/exception std-bad alloc-inheritance.svg

상속 다이어그램

목차

멤버 함수

(생성자)
새로운 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 이후)
(C++26 이후 constexpr)

내용을 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

참고 항목

할당 함수
(함수)