473,503 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new vs. malloc

Compared to malloc(), why would one say that the 'new' operator is type-safe
in a book? Thanks!
Jul 22 '05 #1
4 6308
On Tue, 16 Dec 2003 18:50:34 GMT, "Asapi" <As***@net.net> wrote:
Compared to malloc(), why would one say that the 'new' operator is type-safe
in a book? Thanks!


malloc returns void*, new returns a pointer of the type of the object
created. e.g.

int* ip = malloc(sizeof(short));
//whoops! no compile time error, but runtime one

vs
int* ip = new short;
//compile time error, short* not convertible to int*

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
Asapi wrote:
Compared to malloc(), why would one say that the 'new' operator is type-safe
in a book? Thanks!

"Type-safe" loosely means that if you use the wrong type, the compiler
can tell you about it. Look at the following examples:

int* ip1 = static_cast<int*>(malloc(10 * sizeof(char)));

int* ip2 = static_cast<int*>(new char[10]);

The second line will fail to compile, whereas the first one is just fine
(at least according to the compiler).

- Adam

--
Reverse domain name to reply.

Jul 22 '05 #3
Asapi wrote:
Compared to malloc(), why would one say that the 'new' operator is type-safe
in a book? Thanks!


1. malloc() returns void*, but new returns the proper type.

2. malloc'd objects must be manually {con,de}structed.

Jul 22 '05 #4
On Tue, 16 Dec 2003 19:05:13 +0000, tom_usenet
<to********@hotmail.com> wrote:
On Tue, 16 Dec 2003 18:50:34 GMT, "Asapi" <As***@net.net> wrote:
Compared to malloc(), why would one say that the 'new' operator is type-safe
in a book? Thanks!


malloc returns void*, new returns a pointer of the type of the object
created. e.g.

int* ip = malloc(sizeof(short));
//whoops! no compile time error, but runtime one


Note that the above is C code (where malloc is idiomatic), the C++
would be:

int* ip = static_cast<int*>(malloc(sizeof(short)));
//whoops! no compile time error, but runtime one

which is even more painful to write.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #5

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

Similar topics

19
683
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
6394
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...
231
22922
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
2189
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
10717
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
2557
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
15622
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
2522
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
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...
0
7093
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
7291
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,...
1
7012
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...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
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
4690
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
3180
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
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.