배경
I는 networkprotocol위한 드라이버를 작성하고이 package
std::array<buffer_ptr,2>
되는 함수 write(std::shared_ptr<package> package)
가질하고 (0 => 헤더, 1 => 본체)와 어레이. 편의상 나는 write(buffer_ptr body)
함수를 작성하여 헤더를 자동 생성하고 write
의 첫 번째 양식을 호출한다. 이렇게하려면 std::make_shared
을 보내고 make_shared
호출에서 std::array
을 초기화하는 데 문제가 있습니다.초기화 표준 : make_shared
내가 재판을 어떤 코드
typedef std::shared_ptr<std::vector<uint8_t>> buffer_ptr;
typedef std::array<buffer_ptr, 2> package_t;
typedef std::shared_ptr<package_t> package_ptr;
void connection::write(package_ptr package) {
... //do stuff
}
void connection::write(buffer_ptr body) {
buffer_ptr header = buildHeader(body);
write(std::make_shared<package_t>(???)) //here I want to initialize the array with {header,body}
}
???
는{header, body}
{{header, body}}
std::initializer_list<buffer_ptr>{header, body}
질문 (이러한 오류를 컴파일러 LED) :
이 일을 할 수있는 솔루션이 아니면 내가 좋아하는 뭔가를 작성해야 할 :
package_ptr 패키지를 = 새 패키지 {header, body}; 쓰기 (패키지);
1.b) package_ptr(new package)
에 의존해야 효율성이 떨어지나요? Cppreference에
이다 (I 메모리 요청을 저장하는 포인터와 한 덩어리의 인스턴스에 대한 공유 메모리 할당을 기억하는 것은) 읽
또한왜 메모리가 유출 될 수, F (shared_ptr의 (새 INT (42)) , g()) g가 예외를 throw하면 메모리 누수가 발생할 수 있습니다. make_shared가 인 경우이 문제는 존재하지 않습니다.
(int(42)
라고 g
전에 구축 할 수 있으며, shared_ptr
전에 호출 할 g
라고합니다)? 그리고 1. 대체 코드가 그러한 누출 가능성으로부터 고통을 겪을까요? 먼저
'쓰기 (표준 : make_shared (package_t {헤더, 바디})); ' –
Cubbi
@Cubbi Nice! 마이크로 소프트 컴파일러는 그것을 좋아하지 않았다 : ( –
@Cubbi :'make_shared'가 던져 질 때 (예 : 메모리 부족)? 이미 빌드 된'package_t'가 유출 되었습니까? – ted