By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Selecting between malloc() and new operator

P: n/a
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

Mar 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Amit_Basnak wrote:
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.

--
Ian Collins.
Mar 13 '07 #2

P: n/a
On Mar 13, 6:42 pm, "Amit_Basnak" <Amit.Bas...@gmail.comwrote:
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.

Mar 13 '07 #3

P: n/a
On Mar 13, 5:42 am, "Amit_Basnak" <Amit.Bas...@gmail.comwrote:
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-lit....html#faq-16.4

Cheers! --M

Mar 13 '07 #4

P: n/a
On Mar 13, 5:42 pm, "Amit_Basnak" <Amit.Bas...@gmail.comwrote:
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() ;

Mar 14 '07 #5

P: n/a
On Mar 13, 5:42 pm, "Amit_Basnak" <Amit.Bas...@gmail.comwrote:
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.

Mar 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.