473,508 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting between malloc() and new operator

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
5 1767
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2539
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
16
6756
by: RS | last post by:
Hi, What is the difference between new() and malloc()? RS
6
2415
by: Vinu | last post by:
Hi, I am maintaining a C++ project which is a server which continuously receives requeste from clients. I have noticed that we overload the new operator and in it then call malloc to allocate...
34
6395
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
23
6181
by: puzzlecracker | last post by:
Why is "new" a C++ language-level construct while "malloc" is a library function?
19
2451
by: ceo | last post by:
hi, why do i get the following error? do i need to explicitly typecast the pointer returned by malloc to char *? thanks, ceo # cat malloc.cpp #include <stdio.h>
71
19035
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
2688
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
7
1397
by: Keith Thompson | last post by:
The comp.lang.c-recommended way to invoke malloc() is, of course: some_type *ptr; ptr = malloc(count * sizeof *ptr); But what if (in C99 only) ptr is a pointer to a VLA (variable-length...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.