473,735 Members | 7,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using malloc in C++?

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 problem? I cannot see why using malloc instead of new
does not give the same result.
Jun 7 '07 #1
71 19112
"desktop" <ff*@sss.comwro te in message
news:f4******** **@news.net.uni-c.dk...
>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 problem? I cannot see why using malloc instead of new
does not give the same result.
(Untested code)

class Foo
{
int MyInt;
std::string MyString;
};

int main()
{
Foo* Bar = new Foo;
Foo* Screwed = malloc( sizeof( Foo ) );
}

New will call the constructor for the std::string. malloc won't.
Jun 7 '07 #2

desktop wrote:
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 problem?
The problem comes when you deal with object allocation, in C++, where
new not only allocates memory but also takes the constructor in hand.
>I cannot see why using malloc instead of new
does not give the same result.
For better understanding go through this link :
http://wiki.answers.com/Q/What_is_th...er_than_syntax
Regards,
ar

"There are things known and things unknown, in between are The Doors."

Jun 7 '07 #3
desktop wrote:
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 problem? I cannot see why using malloc instead of new
does not give the same result.
For a bucket of bytes or a C style struct, using malloc/free or
new/delete makes little or no difference. For objects with constructors
and/or destructors you have to use new/delete, so you may as well use
the same for all allocations.

--
Ian Collins.
Jun 7 '07 #4
desktop <ff*@sss.comsch rieb:
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 problem? I cannot see why using malloc instead of
new does not give the same result.
This is a minor problem. The memory returned by new is also
uninitialized in a lot of cases.

Sure, new calls constructors and delete calls destructors while malloc
and free do not.

But there's another major reason for using new and delete: they are
much more portable while malloc/calloc/alloca/realloc/... are not. You
will normally never get a problem using new and delete, also on
different plattforms with different processors, because they
encapsulate all calls to the native malloc functions safely. These
operators belong to the language itself, not to the runtime library.
So you will normally not find includes of different headers and calls
to different allocation functions (both handled by conditional
compilation) and complicated casts for heap objects in strict C++
programs.

T.M.
Jun 7 '07 #5
Jim Langston wrote:
....
class Foo
{
int MyInt;
std::string MyString;
};

int main()
{
Foo* Bar = new Foo;
Foo* Screwed = malloc( sizeof( Foo ) );
}

New will call the constructor for the std::string. malloc won't.
MyInt is still not initialized tho.
Jun 7 '07 #6
Torsten Mueller wrote:
desktop <ff*@sss.comsch rieb:
>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 problem? I cannot see why using malloc instead of
new does not give the same result.

But there's another major reason for using new and delete: they are
much more portable while malloc/calloc/alloca/realloc/... are not. You
will normally never get a problem using new and delete, also on
different plattforms with different processors, because they
encapsulate all calls to the native malloc functions safely. These
operators belong to the language itself, not to the runtime library.
So you will normally not find includes of different headers and calls
to different allocation functions (both handled by conditional
compilation) and complicated casts for heap objects in strict C++
programs.
I don't follow that paragraph, what isn't portable about the malloc
family of standard library functions?

--
Ian Collins.
Jun 7 '07 #7
Ian Collins wrote:
....
I don't follow that paragraph, what isn't portable about the malloc
family of standard library functions?
I don't know if they have been fixed but the following usually caused
problems with different behaviour on different platforms.

free(0)
malloc(0)
Jun 7 '07 #8
Gianni Mariani wrote:
Ian Collins wrote:
....
>I don't follow that paragraph, what isn't portable about the malloc
family of standard library functions?

I don't know if they have been fixed but the following usually caused
problems with different behaviour on different platforms.

free(0)
Specified as a NOP.
malloc(0)
Specified as far as you will either get a pointer to a block of >0 bytes
or NULL, just like any other size.

--
Ian Collins.
Jun 7 '07 #9
Ian Collins wrote:
....
>
>malloc(0)

Specified as far as you will either get a pointer to a block of >0 bytes
or NULL, just like any other size.
That would be a problem then.
Jun 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2277
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ and have some base C knowledge. So I know how to solve a given problem, by I sometimes tend to fall back to C. That is what I want to avoid in my current project. Therefore I have the following question: I maintain a list of objects that are created. There is some information that is logically related to each instance. I encapulate this in a struct like this:
17
2436
by: Johs32 | last post by:
When I make a pointer I have read that if I would like to use it in another function, I need to malloc it first else it will disapear after the function returns. In this code I do not use malloc, but it still works and the function print_me() prints the correct text. Why does it work eventhough I do not use malloc? johs
8
7714
by: jmbn2k | last post by:
Hi Can anyone pls teme how to allocate memory using malloc in a 3 D diemensional array.............
0
1311
by: jmbn2k | last post by:
Hii Can anyone pls teme...how to allocate memory using malloc in a 3 D dimensional array........
13
9723
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
2
1737
by: aziizvu | last post by:
Write a program using malloc function. In which you take input from user and allocate memory equal to square of this number. Which multiply numbers and draw a table in the following format? Hint: User enters 3 then program allocates equal to 9 integer memories. Output:1 Enter a single digit number: 2 The multiplication table of 2 is: 1 2
8
2466
by: digz | last post by:
I saw the following code and comments in a source file, I am not able to appreciate what the author is trying to do in ALIGNED_ALLOC , can some one plz help me understand why CACHE_LINE_SIZE * 2 is first added to the parameters to malloc and then CACHE_LINE_SIZE -1 is added to the return address and after that its bit anded with ( CACHE_LINE_SIZE -1 ) , how is this helping alignment etc ? /*
0
8962
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9463
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6747
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4559
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4822
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2739
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.