Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,527 members, 7,826,980 topics. Date: Tuesday, 14 May 2024 at 02:11 AM

When Do I Use Calloc() And Malloc() In C Language? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / When Do I Use Calloc() And Malloc() In C Language? (507 Views)

An INEC Voting Console Program Written In C Language / How To Allocate Space For Ragged Array Using Calloc() Or Malloc? HELP! / Coding Virus To Kill Computer In C++ Language (2) (3) (4)

(1) (Reply) (Go Down)

When Do I Use Calloc() And Malloc() In C Language? by logicDcoder(m): 12:03pm On Sep 11, 2020
I normally use calloc() (since it allocates memory contiguously which is suitable for arrays) when I want to allocate space/storage for arrays and malloc() when I want to allocate memory space for structures (creating a node for linked list,queues,binary trees etc).
Looking at the implementation of stacks->
Using arrays: each element on the stack is linked with the previous element contiguously.
Using linked list each node(each node is a self referential structure) containing the element are only linked using pointers.
Does it mean that for I to use calloc(), the DS I'm working with must be contiguous in nature.
What happens in the memory when I use calloc() to allocate storage for linked list,queue,tree and other ADTs node.
C++ programmers can also contribute!
Image 1 & 2 -> Implementation of stacks using arrays.
Image 3&4 -> Implementation of stacks using linked list.

Don't mind my English lalasticlala !�

Re: When Do I Use Calloc() And Malloc() In C Language? by valzey(m): 1:50pm On Sep 11, 2020
I think using malloc, the block of memory you have is uninitialized, i.e: whatever data in that space previously will remain there. while in calloc, it is initialized to zero.
Calloc is like a combination of two functions
Malloc and another used to initialize to zero.

Use malloc if you'll use all the blocks allocated and use calloc if you'll use some of the blocks allocated.
Re: When Do I Use Calloc() And Malloc() In C Language? by logicDcoder(m): 9:16pm On Sep 12, 2020
valzey:
I think using malloc, the block of memory you have is uninitialized, i.e: whatever data in that space previously will remain there. while in calloc, it is initialized to zero.
Calloc is like a combination of two functions
Malloc and another used to initialize to zero.

Use malloc if you'll use all the blocks allocated and use calloc if you'll use some of the blocks allocated.


I don't really understand what you mearnt by malloc initializing to zero. I don't really know where you get this. Malloc function is used to allocate storage dynamically for objects. In what I understood malloc() allocates storage uncontigously(malloc returns a pointer to void(generic pointer) which is pointing to a memory block) while calloc() allocates memory contigously.
You aforementioned using malloc() when all the blocks are allocated (which blocks?).
Re: When Do I Use Calloc() And Malloc() In C Language? by logicDcoder(m): 9:28pm On Sep 12, 2020
Just look at the second image.
Line 47.
lemme simplify the code for ya.

struct node *new_node = NULL;//Here i created a pointer which is actually pointing to nothing.

new_node = malloc(sizeof(node));//Here the pointer is now pointing to an actual object of type node here.

You saying that malloc initializes an object to zero is something foreign.
My question is when to use malloc() and calloc?
Should I use calloc() only when allocating storage for arrays or other contigous data structures?
Re: When Do I Use Calloc() And Malloc() In C Language? by valzey(m): 7:24pm On Sep 13, 2020
logicDcoder:



I don't really understand what you mearnt by malloc initializing to zero. I don't really know where you get this. Malloc function is used to allocate storage dynamically for objects. In what I understood malloc() allocates storage uncontigously(malloc returns a pointer to void(generic pointer) which is pointing to a memory block) while calloc() allocates memory contigously.
You aforementioned using malloc() when all the blocks are allocated (which blocks?).

Malloc does not set the bytes it allocates to zero while calloc does. That is their difference.
Other than this, they are the same, they are all contiguous blocks I think.

If you allocate sizeof(char) * 5, and you will use all of the bytes, malloc is OK, if you may not use up all the bytes, then use calloc so that the unused parts will be zero.
Re: When Do I Use Calloc() And Malloc() In C Language? by logicDcoder(m): 8:00am On Sep 14, 2020
valzey:


Malloc does not set the bytes it allocates to zero while calloc does. That is their difference.
Other than this, they are the same, they are all contiguous blocks I think.

If you allocate sizeof(char) * 5, and you will use all of the bytes, malloc is OK, if you may not use up all the bytes, then use calloc so that the unused parts will be zero.

you have a point thanks

(1) (Reply)

Drupal To Wordpress Migration Guide For Merchants / Please Advice Me Please On Laptop For Programming / The Future Of Codeigniter – Why Codeigniter Is Not Dead?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.