Selecting between malloc() and new operator

A

Amit_Basnak

Dear Friends

Currently we are allocating memory in our code to a Structure with
malloc() function and deallocaing using free()

WF_WU_HANDLE * lclStrWorkunit =
(WF_WU_HANDLE*)malloc(sizeof(WF_WU_HANDLE));

where WF_WU_HANDLE is a Structure.

I would like to use new operator as its a C++ code

as in

WF_WU_HANDLE * lclStrWorkunit;
lclStrWorkunit = new WF_WU_HANDLE; // Allocates the memory

then use
delete lclStrWorkunit; // to deallocate the memory

Can someone please explain new suits better than malloc() ?
Thanks
Amit
 
I

Ian Collins

Amit_Basnak said:
Dear Friends

Currently we are allocating memory in our code to a Structure with
malloc() function and deallocaing using free()

WF_WU_HANDLE * lclStrWorkunit =
(WF_WU_HANDLE*)malloc(sizeof(WF_WU_HANDLE));

where WF_WU_HANDLE is a Structure.

I would like to use new operator as its a C++ code

as in

WF_WU_HANDLE * lclStrWorkunit;
lclStrWorkunit = new WF_WU_HANDLE; // Allocates the memory

then use
delete lclStrWorkunit; // to deallocate the memory

Can someone please explain new suits better than malloc() ?

Just look at the code for starters! All those nasty casts and sizeofs
cluttering your code.

malloc just allocates memory, new allocates memory and if the object has
a constructor, constructs the object.
 
S

Sarath

Dear Friends

Currently we are allocating memory in our code to a Structure with
malloc() function and deallocaing using free()

WF_WU_HANDLE * lclStrWorkunit =
(WF_WU_HANDLE*)malloc(sizeof(WF_WU_HANDLE));

where WF_WU_HANDLE is a Structure.

I would like to use new operator as its a C++ code

as in

WF_WU_HANDLE * lclStrWorkunit;
lclStrWorkunit = new WF_WU_HANDLE; // Allocates the memory

then use
delete lclStrWorkunit; // to deallocate the memory

Can someone please explain new suits better than malloc() ?
Thanks
Amit

In addition to Ian Collins comment, you should use the same allocation/
deallocation mechanism. Don't mix up the allocation/deallocation
facilities.
i.e if you are using new, you have to use same for of delete.
if you are using malloc, you should use it's corresponding function
(free) to free up the memory.
If you are using C memory management functions with C++ objects, the
constructor and destructors will not be called.
 
M

mlimber

Dear Friends

Currently we are allocating memory in our code to a Structure with
malloc() function and deallocaing using free()

WF_WU_HANDLE * lclStrWorkunit =
(WF_WU_HANDLE*)malloc(sizeof(WF_WU_HANDLE));

where WF_WU_HANDLE is a Structure.

I would like to use new operator as its a C++ code

as in

WF_WU_HANDLE * lclStrWorkunit;
lclStrWorkunit = new WF_WU_HANDLE; // Allocates the memory

then use
delete lclStrWorkunit; // to deallocate the memory

Can someone please explain new suits better than malloc() ?

See the FAQ:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.4

Cheers! --M
 
W

wufeng

Dear Friends

Currently we are allocating memory in our code to a Structure with
malloc() function and deallocaing using free()

WF_WU_HANDLE * lclStrWorkunit =
(WF_WU_HANDLE*)malloc(sizeof(WF_WU_HANDLE));

where WF_WU_HANDLE is a Structure.

I would like to use new operator as its a C++ code

as in

WF_WU_HANDLE * lclStrWorkunit;
lclStrWorkunit = new WF_WU_HANDLE; // Allocates the memory

then use
delete lclStrWorkunit; // to deallocate the memory

Can someone please explain new suits better than malloc() ?
Thanks
Amit

malloc() just allocates memory, and operator ::new do something like
allocates memory and invokes the object constructor if it has a valid
constructor.

class A
{
public:
A()
{
m = 0 ;
}

int m ;
}

A* p = new A() ;

equal

A* p = (A*)::malloc(sizeof(A)) ;
p->A::A() ;
 
S

sss.zhou

Dear Friends

Currently we are allocating memory in our code to a Structure with
malloc() function and deallocaing using free()

WF_WU_HANDLE * lclStrWorkunit =
(WF_WU_HANDLE*)malloc(sizeof(WF_WU_HANDLE));

where WF_WU_HANDLE is a Structure.

I would like to use new operator as its a C++ code

as in

WF_WU_HANDLE * lclStrWorkunit;
lclStrWorkunit = new WF_WU_HANDLE; // Allocates the memory

then use
delete lclStrWorkunit; // to deallocate the memory

Can someone please explain new suits better than malloc() ?
Thanks
Amit

This question likes I should use c++ or c.

Instead of using new, you use malloc in all of your c++ code , I
suggest you should use c.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top