type casting

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 31
Reputation: Sukhbir is an unknown quantity at this point 
Solved Threads: 0
Sukhbir Sukhbir is offline Offline
Light Poster

type casting

 
0
  #1
Oct 19th, 2004
what happened if we do't typecast return value of malloc
like that

int *a=malloc(10);
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 235
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: type casting

 
0
  #2
Oct 19th, 2004
Casting malloc
  1. int *a = malloc(10 * sizeof *a);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: type casting

 
0
  #3
Oct 19th, 2004
Most compilers will complain because malloc() returns void* and, in C++, you need to cast void* to the type you are mallocing.

Generally, you should use 'new' and 'delete' rather than malloc, for at least these three good reasons:
1) You don't need to cast
2) As Dave pointed out, malloc(10) allocates 10 BYTES, not 10 ints
3) Constructors are not called. Not a big deal with ints, but a huge deal with objects.

In this case,

int* a = new int[10];
delete [] a;

note the need for [] in the delete; if you used [] in new, use them in delete.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: type casting

 
0
  #4
Oct 19th, 2004
Using C memory allocation routines in C++ programs is BAD FORM.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: type casting

 
0
  #5
Oct 20th, 2004
>int *a = malloc(10 * sizeof *a);
Originally Posted by Chainsaw
Most compilers will complain because malloc() returns void* and, in C++, you need to cast void* to the type you are mallocing.

Generally, you should use 'new' and 'delete' rather than malloc, for at least these three good reasons:
1) You don't need to cast
2) As Dave pointed out, malloc(10) allocates 10 BYTES, not 10 ints
3) Constructors are not called. Not a big deal with ints, but a huge deal with objects.

In this case,

int* a = new int[10];
delete [] a;

note the need for [] in the delete; if you used [] in new, use them in delete.
>Using C memory allocation routines in C++ programs is BAD FORM.
I see nothing in the original post that suggests either C++ or C. As the answer is directly related to the language, don't you three think it makes sense to ask before going off on your favorite tangent?

>what happened if we do't typecast return value of malloc
In C, nothing because pointers are implicitly converted to and from void without the need for a cast. In fact, it is best to avoid the cast in C because it hides errors.

In C++, you would get a compile-time error because C++ does not bless implicit pointer conversions to and from void. A cast is required, but it's better to use new in C++ for several reasons that I won't mention because that wasn't your question.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC