Re: About C++ memory management
- From: "Alexander Nickolov" <agnickolov@xxxxxxxx>
- Date: Tue, 30 Oct 2007 17:14:58 -0700
If you want to properly maintain pointer aliasing, use smart
pointers like boost::shared_ptr<>.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Kürþat" <xx@xxxxxx> wrote in message
news:OwdaQ0zGIHA.3600@xxxxxxxxxxxxxxxxxxxxxxx
Hi,
I have some questions about C++ memory management:
- I know that every pointer allocated by using new operator should be
deleted:
int * ptr = new int;
.
.
delete ptr;
But if I copy this pointer like below should I also delete copied pointer?
int * ptr = new int;
int * ptr1 = ptr
.
.
delete ptr;
delete ptr1;????
---------------------------------------------------
- Consider I have a class which have a pointer member which is supplied by
the caller :
class Foo
{
private:
int * m_ptr;
public:
Foo (int * ptr)
{
m_ptr = ptr;
}
}
Now let us see two situations :
A) int n;
Foo foo (&n);
B) int * ptr;
ptr = new int;
Foo foo (ptr)
In A, there is no need to delete passed pointer in the calss but in B
passed pointer should be deleted by the class, shouldn't it? If yes,how
can I know what type of allocation is made by the caller when I design
that class?
Thanks in advance.
.
- References:
- About C++ memory management
- From: Kürşat
- About C++ memory management
- Prev by Date: Re: problem in timings using thread in VC++
- Next by Date: URLDownloadToFile and IHttpNegotiate::OnResponse
- Previous by thread: Re: About C++ memory management
- Next by thread: URLDownloadToFile and IHttpNegotiate::OnResponse
- Index(es):
Relevant Pages
|