I have been working on C++ code optimization to minimize the memory leaks and better performance on HP-UX with aCC
I have the following example to share with you
I have a structure SYS_LC_DETAILS_CDA for which I have allocated a memory using malloc and then memset and then calling a function to assign values to the structure elements as below
SYS_LC_DETAILS_CDA *p_str_lc_details = (SYS_LC_DETAILS_CDA *)malloc (sizeof(SYS_LC_DETAILS_CDA));
memset(p_str_lc_details,'\0',sizeof(SYS_LC_DETAILS_CDA));
conObj.CopyToDceSYS_LC_DETAILS_CDA(p_str_lc_details,pStrLcDetails);
and calling free()
in another example
im using a new operator as below
SYS_LC_DETAILS_CDA *p_str_lc_details = new SYS_LC_DETAILS_CDA ;
memset(p_str_lc_details,'\0',sizeof(SYS_LC_DETAILS_CDA));
conObj.CopyToDceSYS_LC_DETAILS_CDA(p_str_lc_details,pStrLcDetails);
then delete
in a 3rd way
I do as below
SYS_LC_DETAILS_CDA p_str_lc_details;
memset(&p_str_lc_details,'\0',sizeof(SYS_LC_DETAILS_CDA));
in above case I dont have to deallocate any...
in a 4th way I am using an auto_ptr as below
auto_ptr<SYS_LC_DETAILS_CDA>p_str_lc_details = (new
SYS_LC_DETAILS_CDA) ;
and then passing the pointer variable as
conObj.CopyToDceSYS_LC_DETAILS_CDA(p_str_lc_details,pStrLcDetails);
Which is the better way of allocating . auto_ptr score well above the rest as the object gets destroyed as soon as it goes out of scope ... So which is the best way to minimize the meamory leaks ?
Many Thanks