473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

malloc - to cast or not to cast, that is the question...

I've always casted the result of a call to c/malloc viz.:

int * ptr = (int *) calloc(10, sizeof(int));

However, I know this is not strictly necessary as there is always an implied
cast on the void pointer that is returned. A potential reason for not
casting was pointed out to me today by Martin Dickopp (cheers Martin). I
just wondered what the thoughts of the group were on this.

Cheers,

--

EvilRix
Nov 14 '05 #1
8 1862
On Fri, 13 Feb 2004 06:55:24 -0000, "EvilRix"
<evilrix@[@@@]hotmail.com> wrote:
I've always casted the result of a call to c/malloc viz.:

int * ptr = (int *) calloc(10, sizeof(int));

However, I know this is not strictly necessary as there is always an implied
cast on the void pointer that is returned. A potential reason for not
casting was pointed out to me today by Martin Dickopp (cheers Martin). I
just wondered what the thoughts of the group were on this.


Back up a few posts and you'll find two threads (one _very_ long)
discussing this very issue. There's really no reason to start this
religious war all over again.

Or, HIBT?

--
Sev
Nov 14 '05 #2
EvilRix wrote:
I've always casted the result of a call to c/malloc viz.:

int * ptr = (int *) calloc(10, sizeof(int));

However, I know this is not strictly necessary as there is always an implied
cast on the void pointer that is returned. A potential reason for not
casting was pointed out to me today by Martin Dickopp (cheers Martin). I
just wondered what the thoughts of the group were on this.


This is old ground too often gone over. If you really cared what people
here thought, you would have followed the newsgroup, checked the
backtraffic (groups.google.com is good for this), and check the FAQ.

The canonical comp.lang.c approach to the above is, after #including <stdlib.h>
int *ptr = calloc(10, sizeof *ptr);
with appropriate checks afterward to be sure that calloc succeeded.
Something like
if (!ptr) { /* code to handle failure */ }
Using calloc instead of malloc makes sense only if the all-bits-zero result
is important to you, and be sure that you are not better served with
int array[10] = {0};
The magic constant '10' above should probably be moved to a #define, and,
should you use any of malloc, calloc, realloc, remember to free the space
when done.

--
Martin Ambuhl
Nov 14 '05 #3
EvilRix wrote:
I've always casted the result of a call to c/malloc viz.:

int* ptr = (int*)calloc(10, sizeof(int));
Good idea. So do I.
However, I know this is not strictly necessary as
there is always an implied cast on the void pointer that is returned.
A potential reason for not casting was pointed out to me today
by Martin Dickopp (cheers Martin).
I don't know what reason Martin Dickopp gave to you
but it was probably bogus.
I just wondered what the thoughts of the group were on this.


Nov 14 '05 #4
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
EvilRix wrote:
I've always casted the result of a call to c/malloc viz.:
int* ptr = (int*)calloc(10, sizeof(int));


Good idea. So do I.
However, I know this is not strictly necessary as
there is always an implied cast on the void pointer that is returned.
A potential reason for not casting was pointed out to me today
by Martin Dickopp (cheers Martin).


I don't know what reason Martin Dickopp gave to you
but it was probably bogus.


Hello EvilRix,

Do you notice how strangely convinced E. Robert Tisdale is that the
reason is bogus, despite his claim of not knowing what it is? Draw
your own conclusions. :)

Martin
Nov 14 '05 #5
Martin Dickopp wrote:
E. Robert Tisdale writes:
I don't know what reason Martin Dickopp gave to you
but it was probably bogus.
Hello EvilRix,

Do you notice how strangely convinced E. Robert Tisdale is that
the reason is bogus,
despite his claim of not knowing what it is?


Do you know what the "reason" is?
Draw your own conclusions. :)


My conclusion is that
something that calls itself EvilRix is your troll mate. :-)

You can continue to feed it if you like but count your fingers.

Nov 14 '05 #6
On Fri, 13 Feb 2004 16:43:49 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
Martin Dickopp wrote:
E. Robert Tisdale writes:
I don't know what reason Martin Dickopp gave to you
but it was probably bogus.


Hello EvilRix,

Do you notice how strangely convinced E. Robert Tisdale is that
the reason is bogus,
despite his claim of not knowing what it is?


Do you know what the "reason" is?
Draw your own conclusions. :)


My conclusion is that
something that calls itself EvilRix is your troll mate. :-)

You can continue to feed it if you like but count your fingers.


Please ignore the troll.

WWKKWWWWWWKKWWWWWWKKWWWWWWKKWWKKWWWWKKWWWWKKWWWWWW WWKKWWWWKKWWWWWWWWWW
WWWWWWKKWWWWWWKKWWWWWWKKWWWWWWWWWWWWWWWWWWWWWWKKWW WWWWWWWWW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;ffGGffffLLtt,,tttt:: WWWWKKWWWW
WWWWWWKKWWWW,,,,;;;;;;iiiiiiiiiitttt,,;;LLtttttt,, ;;ttLL,,WWKKWWWWWWWW
WWKKWWWWWWKKii,,;;ii;;;;,,;;,,iiii;;;;,,jjjjii;;;; ;;ttGG::WWWWWWWWKKWW
WWWWWWKKWWWWttiiiiii;;;;;;;;ttLLffttjjLLGGDDffiiii ttttLL WWKKWWWWWWWW
WWKKWWWWWWWWiiii;;;;;;ii;;ttGGLLEEEEEEKKKKDDLLjjff ffffff..WWWWWWKKWWWW
WWWWWWKKWWWWjjtt;;iiiiiittLLGGffLLLLDDEEDDDDGGLLff LLGGGG;;WWKKWWWWKKWW
WWKKWWWWKKWW;;iiiiiiiiiiffDDDDLLLLLLGGGGDDEEEEGGff GGGGGGttWWWWWWWWWWWW
WWWWWWWWWWWW,,iiiiiittttffDDEEDDEEEEGGEEKKKKEEffLL DDDDDDffKKWWKKWWKKWW
WWKKWWKKWW,,,,iittiiiittffGGttttttffLLGGGGGGLLjjLL DDEEEEttWWWWWWWWWWWW
WWWWWWWWiiiiiijjjjjjttffffffjjttjjGGGGGGLLGGLLGGDD DDEEEE;;WWWWKKWWKKWW
WWKKWWfftt;;LLjjffLLffffLLLLjjiittttjjLLffLLDDEEEE EEEEff;;iiWWWWWWWWWW
WWWWWW;;..ttEEttffLLGGGGLLLLffjjttttjjffLLDDEEKKKK KKKKLL,,ttttWWKKWWWW
WWWW::..,,ffEEffjjLLLLLLGGDDGGLLffLLLLLLDDEEKKKKKK EEKKDD,,::;;;;WWWWii
,:....;;ffLLEEDDttffLLGGLLLLGGEEEEEEEEKKKKKKKKEEKK EEEEEEii;;..,,,,;;tt
.....::jjGGGGDDEEffjjffLLLLffffffGGEEEEEEEEEEKKEEK KEEKKEEfftt;;::..::tt
...::;;jjDDDDGGEEGGiijjffGGffLLLLLLGGGGDDEEKKEEKKE EKKKKDDLLjjtt;;::::..
,,,,iiiiEEDDDDDDDDttttffLLLLLLGGLLGGDDEEEEEEKKEEDD KKEEDDffjjLLGGtt,,..
,,;;ttffDDEEDDDDEELLttjjLLLLLLGGLLDDEEEEEEEEEEDDDD KKDDLLffGGEEEEtt,,::
,,;;iiGGGGEEKKEEEEDDjjttffffLLLLDDGGDDDDEEGGDDDDEE GGLLjjDDEEKKLLii,,,,
;;iiiiLLEEDDEEEEDDDDffffffjjffLLGGGGDDGGLLGGGGDDLL GGLLEEEEKKGGjjii;;;;
iiiittttLLEEEEEEKKKKjjttffffjjffffffjjffffLLDD;;GG KKKKKKEEDDffttii;;;;
iiiiiittffLLGGDDEEKKDDffffffjjjjttttiittffjjiittKK KKEEEEGGLLttiiii;;;;
iiiittiiffffjjffjjLLDDEEDDLLttjjjjjjttjjttffKKKKDD GGGGLLLLttttiiii;;;;
iiiiiittttjjffjjffffffLLDDEEEELLffffjjttDDKKKKDDGG GGffffffttiiii;;;;ii
iiiiiittttttjjjjjjjjffffffLLLLGGDDffffEEEEDDGGGGLL LLffjjttiiii;;iiiiii

--
Sev
Nov 14 '05 #7
E. Robert Tisdale wrote:
EvilRix wrote:
I've always casted the result of a call to c/malloc viz.:

int* ptr = (int*)calloc(10, sizeof(int));

Good idea. So do I.


I hope "EvilRix" means something malicious, because you have just acquired
the most evil bedfellow here when ERT agrees with you.
However, I know this is not strictly necessary as
there is always an implied cast on the void pointer that is returned.
A potential reason for not casting was pointed out to me today
by Martin Dickopp (cheers Martin).


I don't know what reason Martin Dickopp gave to you
but it was probably bogus.


That Martin Dickopp often gives good advice and ERT almost never does, and
that ERT is ready to condemn MD's advice sight-unseen should give you a
clue. There is a running score going of the few times ERT has been right
about anything: the number is small.

--
Martin Ambuhl
Nov 14 '05 #8
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
My conclusion is that something that calls itself EvilRix is your
troll mate. :-)


It's a honor to be called a troll by you. Thank you!

Martin
Nov 14 '05 #9

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

Similar topics

59
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h>...
34
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
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...
66
by: Knady | last post by:
Hi, I have the following problem, I must to do my assignment, but I really do not know how to use the malloc. I need create a program that will be used to do some algebrical computation on the...
36
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program...
68
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;...
19
by: SP | last post by:
I am learning C and have a question re: malloc(). I wrote simple program which assigns a value to a structure and then prints it as follow: #include <stdio.h> #include <stdlib.h> struct...
41
by: SRR | last post by:
Why is it discouraged to explicitly typecast the void pointer returned by malloc(); function? For example: { int *p; p = (int*)malloc(2*sizeof(int)); /*Explicit casting is done, therfore it...
35
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
This topic is a FAQ. But I have read the faq and spent a couple of hours browsing the group archives, and still have a few questions that I hope you can answer. My understanding is that...
10
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...

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.