944,153 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5091
  • C++ RSS
Oct 19th, 2004
0

type casting

Expand Post »
what happened if we do't typecast return value of malloc
like that

int *a=malloc(10);
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Sukhbir is offline Offline
31 posts
since Jul 2004
Oct 19th, 2004
0

Re: type casting

Casting malloc
C++ Syntax (Toggle Plain Text)
  1. int *a = malloc(10 * sizeof *a);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 19th, 2004
0

Re: type casting

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Oct 19th, 2004
0

Re: type casting

Using C memory allocation routines in C++ programs is BAD FORM.
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
Oct 20th, 2004
0

Re: type casting

>int *a = malloc(10 * sizeof *a);
Quote 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Difficulty understanding Object Orientated File Streaming
Next Thread in C++ Forum Timeline: Help Writing Fuction to Read Fractions From User





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC