Re: Better way of allocating a memory( auto_ptr vs new vs malloc)
Ami wrote:
I have few qustions
Consider the allocation SYS_LC_DETAILS_CDA p_str_lc_details
(SYS_LC_DETAILS_CDA());
SYS_LC_DETAILS_CDA() in above would do default initialization right
then I can safely avoid the use of C style memset..
and then nevere have to de allocate the same.
As far as auto_ptr is concrned , do I again need to initialize to
SYS_LC_DETAILS_CDA () as shown below
auto_ptr<SYS_LC_DETAILS_CDA>p_str_lc_details = (new SYS_LC_DETAILS_CDA
()) ;
another major concerned abt using auto_ptr is
after the memory allocation is done tht is lets say
int main ()
{
auto_ptr<SYS_LC_DETAILS_CDA>p_str_lc_details = (new SYS_LC_DETAILS_CDA
()) ;
conObj.CopyToDceSYS_LC_DETAILS_CDA(p_str_lc_details.get
(),pStrLcDetails); //
return ( EXIT_SUCESS);
}
void CopyToDceSYS_LC_DETAILS_CDA(SYS_LC_DETAILS_CDA *output)
{
// some code
}
do u see any problem with uisng an auto pointer like above i.e passing
as an argument in a function call .
Is there any memory Leak ?
I think the auto of auto ptr is the best way ..
Also in the code the malloc is done like
SYS_GUARANTORS_CDA *p_str_guarantors = (SYS_GUARANTORS_CDA *)malloc
(sizeof(SYS_GUARANTORS_CDA)*(p_nbr_Guarantors+1));
so using new for above is like
SYS_GUARANTORS_CDA *p_str_guarantors = (SYS_GUARANTORS_CDA *)new char
[sizeof(SYS_GUARANTORS_CDA)*(p_nbr_Guarantors+1)];
is the above correct way i.e using an array
Thanks
My god that is so hard to read. Look at the difference a couple of
find-and-replaces and some indention do:
I have few qustions
Consider the allocation
T p(T());
T() in above would do default initialization right
then I can safely avoid the use of C style memset..
and then nevere have to de allocate the same.
As far as auto_ptr is concrned , do I again need to initialize to
T () as shown below
auto_ptr<T>p = (new T()) ;
another major concerned abt using auto_ptr is
after the memory allocation is done tht is lets say
int main ()
{
auto_ptr<T>p = (new T()) ;
func(p.get()); //
return ( EXIT_SUCESS);
}
void func(T *output)
{
// some code
}
do u see any problem with uisng an auto pointer like above i.e passing
as an argument in a function call .
Is there any memory Leak ?
I think the auto of auto ptr is the best way ..
Also in the code the malloc is done like
T *p = (T *)malloc (sizeof(T)*(n+1));
so using new for above is like
T *p = (T *)new char [sizeof(T)*(n+1)];
is the above correct way i.e using an array
Long names greatly hurt readability here.