Docsity

Difference Between Malloc and Calloc in C programming, Summaries of C programming

Toggle sidebar
bg1
DIFFERENCE BETWEEN MALLOC() AND CALLOC() IN C
Both malloc() and calloc() are functions used to allocate dynamic memory during
program execution. However, they differ in how they allocate and initialize this memory.
1. malloc() (Memory Allocation)
• Purpose: Allocates a specified number of bytes of memory.
• Initialization: The allocated memory contains indeterminate data (garbage
values). It is not initialized to zero.
• Syntax:void* malloc(size_t size);
• Example:int* ptr = (int*) malloc(5 * sizeof(int));
This allocates memory for 5 integers, but their values are garbage until
initialized.
2. calloc() (Contiguous Allocation)
• Purpose: Allocates memory for an array of elements, initializes all bytes to zero.
• Initialization: The allocated memory is initialized to zero.
• Syntax:
void* calloc(size_t num, size_t size);
o num: number of elements
o size: size of each element
• Example:
int* ptr = (int*) calloc(5, sizeof(int));
This allocates memory for 5 integers, and all values are initialized to zero.
pf3

Partial preview of the text

Download Difference Between Malloc and Calloc in C programming and more Summaries C programming in PDF only on Docsity!

DIFFERENCE BETWEEN MALLOC() AND CALLOC() IN C

Both malloc() and calloc() are functions used to allocate dynamic memory during program execution. However, they differ in how they allocate and initialize this memory.

1. malloc() (Memory Allocation)

  • Purpose: Allocates a specified number of bytes of memory.
  • Initialization: The allocated memory contains indeterminate data (garbage values). It is not initialized to zero.
  • Syntax: void* malloc(size_t size);
  • Example: int* ptr = (int*) malloc(5 * sizeof(int)); This allocates memory for 5 integers, but their values are garbage until initialized.

2. calloc() (Contiguous Allocation)

  • Purpose: Allocates memory for an array of elements, initializes all bytes to zero.
  • Initialization: The allocated memory is initialized to zero.
  • Syntax: void* calloc(size_t num, size_t size); o num: number of elements o size: size of each element
  • Example: int* ptr = (int*) calloc(5, sizeof(int)); This allocates memory for 5 integers, and all values are initialized to zero.

Key Differences Summarized

Feature malloc() calloc() Memory allocation Allocates uninitialized memory Allocates memory and initializes it to zero Syntax malloc(size_t size) calloc(size_t num, size_t size) Initialization No (contains garbage data) Yes (all bits set to zero) Typical use case When you plan to overwrite all data immediately When you want allocated memory to start with zero values

Practical Examples

Example 1: Using malloc() #include #include int main() { int* arr = (int*) malloc(3 * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed.\n"); return 1; } // Values are garbage initially printf("Values before initialization: %d %d %d\n", arr[0], arr[1], arr[2]); // Initialize the array arr[0] = 10; arr[1] = 20; arr[2] = 30; printf("Values after initialization: %d %d %d\n", arr[0], arr[1],