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

Recommended Answers

All 5 Replies

Either way you are going to have to make sure you clean up all memory assignments. The bonus of new/delete over malloc/calloc/realloc/free is the fact that new and delete call constructors and destructors of classes (potentially allowing you to clean more memory up easier. This was also the reason for using new and delete (i believe) since there needed to be a way of calling constructers when allocating memory) which is a big bonus.

Other than that, as far as i am aware the only difference is synatical. Plus new doesn't require type casting!

If anybody could shed some more light on this i would also be grateful!

Chris

Chris Thanks

My main focus is on the 3rd and 4th way
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);

auto_ptr is effective way to make sure that no leaks happen ..
also
using like
SYS_LC_DETAILS_CDA p_str_lc_details;
and then doing a memset is also effective
let me know

As you stated the advantage of auto_ptr's is the fact they deal with cleans up's automatically. Such as if an object was created with new, and delete was never called for any reason such as program terminated before delete was called for any reason. Then indeed you have a memory leak! Where as no matter what auto pointers with delete any object they are assigned too.

However one dis advantage to the auto_ptr is the fact it doesn't like to be mixed with standard containers such as verctors. Also you cannot copy* an auto_ptr as the copies created are never an exacted copy. As you can imagine thats gonna end badly for everyone!

As for method 3...i'm not sure as thats even a pointer...you simple clear any contents at its memory location using memset...

Chris

Thanks Chris
I have got few question..
Passing auto_ptr by value in a function call is always an invitation to disaster .. Conside a sample code example as below
nt main ()
{
auto_ptr<T>p = (new T()) ;
func(p.get()); // Passing by Value
p.release(); // Would free up
return ( EXIT_SUCESS);
}
void func(T *ptr)
{
ptr->do_Something();
}

When func()'s parameter ptr is initialized by calling AutoPtr's constructor, ownership of the object pointed to by p is transferred to "ptr" . When func() exits, ptr goes out of scope and its destructor deletes memory area of object T. p now holds the dangling pointer and any attempt to use p will most likely cause a core dump.
Can the above be avoided by passing a reference in a function call
as below
func(&(p.get())); // Passing by Reference ..

Another question is
If an exception is thrown in func () .. what would be the result ..
How to handle this ??
Many Thanks

This is what I have now Passing auto_ptr by reference
int main ()
{
auto_ptr<T>p = (new T()) ;
func(p); // appending get() wont work in a function call
p.release();
return ( EXIT_SUCESS);
}
void func(const auto_ptr<T> &output)
{
output->do_Something();
}

works just fine..
Can you please let me know if this is the best way of passing auto_ptr by reference and avoiding any leaks ?
also why get() doesnt work in a function call func(p.get()) // throws error ?
my main question is what happens when an exception is thrown in func()?

Many Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.