2016-10-29 3 views
-1

내가 작업 ... pair 개체에 대한 을 메모리를 할당하려고 그리고 이런 식으로 삭제하고 있습니다 :메모리 누수가

#include <iostream> 
#include <utility> 

int 
main(int argc, char **argv) 
{ 
    std::pair <int, int> *m_pair= new std::pair<int, int>(1, 10); 
    std::cout << "First : " << m_pair->first 
     << " Second : " << m_pair->second << std::endl; 
    delete m_pair; 
    return 0; 
} 

그러나 Valgrind의이 불평 다른 무엇 메모리 누수 ... 입니다 나는 자유해야합니까?

Valgrind output is 
==14145== HEAP SUMMARY: 
==14145==  in use at exit: 72,704 bytes in 1 blocks 
==14145== total heap usage: 2 allocs, 1 frees, 72,712 bytes allocated 
==14145== 
==14145== LEAK SUMMARY: 
==14145== definitely lost: 0 bytes in 0 blocks 
==14145== indirectly lost: 0 bytes in 0 blocks 
==14145==  possibly lost: 0 bytes in 0 blocks 
==14145== still reachable: 72,704 bytes in 1 blocks 
==14145==   suppressed: 0 bytes in 0 blocks 
==14145== Rerun with --leak-check=full to see details of leaked memory 

call_init.part 것은

+3

메모리 누출이 없습니다. valgrind 결과는 무엇입니까? –

+0

@EliSadoff 질문 업데이트 됨 ... – Goutam

+1

여기에 메모리 누수가 없습니다. 당신은 분명히, 간접적으로, 아마도 0 바이트를 잃어 버렸을 것입니다. –

답변

-1

나는 누출을 볼 수 없습니다. 참조 된 카운트 된 포인터를 사용하고 새로운 & 삭제를 잃는다면 하나를 얻습니까? 새로운 패밀리 함수를 만들거나 C++14의 원시 포인터에 대한 스마트 포인터를 사용하면 메모리 관리에 대해 전혀 염려 할 필요가 없습니다.

#include <iostream> 
#include <utility> 
#include <memory> 

int main(int argc, char **argv) 
{ 
    auto m_pair = std::make_unique<std::pair<int,int>>(0, 1); 
    std::cout << "First : " << m_pair->first 
       << " Second : " << m_pair->second << std::endl; 
    return 0; 
}