Can I please have a good detailed explanation on the differences between malloc and calloc? I always have trouble understanding that.
COKEDUDE
27
Junior Poster in Training
Recommended Answers
Jump to Postcalloc is the same as malloc but calloc initializes the memory to all 0s
Both these are the same
int *array = malloc(10 * sizeof(int)); for(int i = 0; i < 10; i++) array[i] = 0; The above can be rewritten with this one line: int *array …
Jump to PostReturn type of both is void so don't we have to type cast it ??
Or by directly assigning to 'int' type pointer it is automatically type-casted ?
Jump to PostC presumes malloc returns int and silently casts your 64 bit pointer to a 32 bit integer.
Just because the program is running on a 64-bit operating system doesn't mean malloc() will return a 64-bit pointer. It depends on the compiler, not the operating system. And if the …
All 10 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Rahul47
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
Rahul47
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Banfa
597
Posting Pro
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
naveen1993
-3
Newbie Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.