answersLogoWhite

0

Difference beween calloc malloc and realloc?

Updated: 8/16/2019
User Avatar

Wiki User

14y ago

Best Answer

There are two differences. First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

Here are more opinions and answers from FAQ Farmers:

  • The difference between malloc and calloc are: 1. malloc() allocates byte of memory, whereas calloc()allocates block of memory.
  • Calloc(m, n) is essentially equivalent to p = m*malloc(n); memset(p, 0, m * n); The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values. Free is properly used to free the memory allocated by calloc.
  • Malloc(s); returns a pointer for enough storage for an object of s bytes. Calloc(n,s); returns a pointer for enough contiguous storage for n objects, each of s bytes. The storage is all initialized to zeros.
  • Simply, malloc takes a single argument and allocates bytes of memory as per the argument taken during its invocation. Where as calloc takes two aguments, they are the number of variables to be created and the capacity of each vaiable (i.e. the bytes per variable).

This one is false:

  • I think calloc can allocate and initialize memory, if the asked memory is available contiguously where as malloc can allocate even if the memory is not available contiguously but available at different locations.

malloc will allocate a block of memory

realloc will resize a block of memory

// allocate a pointer to a block of memory to hold 10 ints

int *intArray = malloc(10 * sizeof(int));

// change intArray to hold 15 ints

intArray = realloc(15 * sizeof(int));

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference beween calloc malloc and realloc?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What if happen when you allocate memory by using malloc or calloc when the momory is not available contigiously?

malloc/calloc/realloc will return NULL


Which function should be used to free the memory allocated by calloc?

Use the free function to release memory that was previously allocated by malloc, calloc or realloc.


What are the different types of memory allocations in c?

1.malloc 2.calloc 3.realloc 4.free


What is the malloc function?

The malloc function is one of a group of memory allocation functions; malloc, calloc, realloc, and free. Specifically malloc (size) returns a pointer to a new memory block of at least size bytes, suitably aligned for optimum access for any basic type, or it returns a NULL if the request cannot be satisfied.


What is the difference between Malloc and calloc in C and JAVA?

In C, malloc is used to reserve a predetermined size of memory. void * malloc ( size_t size ); calloc is used to reserve a chunk of memory large enough to store num elements, each of a predetermined size. void * calloc ( size_t num, size_t size ); To create a char array of size 10 you can do it in one of two ways: char* mChars = malloc( 10 * sizeof(char) ); char* cChars = calloc( 10, sizeof(char) ); There is no concept of malloc or calloc in Java.


Which of libraries calloc belongs?

malloc()


Difference between malloc and alloc?

alloc is used as header file while using malloc and calloc functions in C prog.The definition of malloc function is in the alloc.h file.It's stdlib.h to be more precise


What is malloc vs calloc?

malloc allocate a memory section whereas memset manipulate the content of the memory section, (for example fill a memory section pointed by pointer ptr with 0, we use memset(ptr,0,sizeof(ptr_data_type)) A memory section must be allocated(using either 'malloc' or 'new' in C++) before memset can be used on it.


Contiguous memory allocation program in Linux?

malloc or calloc


How can you allocate memory dynamically in c?

char* new_string; // could be any type new_string = (char*) malloc (5120); // allocate memory - typecast is necessary if (new_string == NULL) ... memory exception ... ... use the data ... free (new_string); // release memory when done


In which way calloc is better than malloc?

What calloc does is: void *calloc (size_t s1, size_t s2) { size_t s= s1*s2; void *p= malloc (s); if (p && s) memset (p, 0, s); return p; }


71 Which function should be used to free the memory allocated by calloc?

free() is a function used to free the memory allocated dynamically ,by both malloc and calloc functions. free(ptr): ptr is a pointer to a memory block which has already been creeated by malloc or calloc.