calloc function --dynamic allocation of memory and zero initialization

Memory can be allocated dynamically by using the calloc function. The memory area is initialized to 0. You need to load "stdlib.h".

#include <stdlib.h>

void * calloc (size_t n, size_t size);

The calloc function allocates memory in the heap area.

The first argument specifies the size of the data in one unit, in bytes. The second argument specifies the number of data. These types are of type size_t. The size_t type has a different width depending on the environment, but it is defined by an unsigned integer of 16 bits or more and the C language specification. In a modern environment, it is safe to specify a value in the range of 0 to Signed 32-bit integer type maximum value (0 to 2147483647). I assume that. (If there is an exceptional environment, please thermy).

The return value is the address at the beginning of the reserved memory area. The return type is the generic pointer type "void *". You can assign it to any pointer type.

When allocating dynamic memory for general applications and libraries, we recommend the calloc function, which is initialized to 0, rather than the malloc function.

When you have finished using the memory area allocated by calloc, use the free function to free it. If you forget to release it, a memory leak will occur.

sample calloc function

Let's write a sample calloc function.

Dynamic array generation

This is a sample that dynamically generates array.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  // Allocate a memory area of ​​10 length with the size of int32_t (4 bytes * 10 = 40 bytes)
  int32_t nums_length = 10;
  int32_t * nums = calloc (sizeof (int32_t), nums_length);
  
  // Array operations
  nums [0] = 10;
  printf("%d\n", nums [0]);
  
  // Release
  free (nums);
}

Dynamic generation of structure data

This is a sample that dynamically generates the data of structure.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// Definition of a structure that represents book information
struct myapp_book {
  int32_t id;
  const char * name;
  int32_t price;
};;

int main (void) {
  // Structure dynamic memory allocation (calloc)
  struct myapp_book * book = calloc (sizeof (struct myapp_book), 1);
  book->id= 1;
  book->name= "C99 Book";
  book->price= 2000;
  
  printf("id:%d, name:%s, price:%d\n", book->id, book->name, book->price);
  
  // Memory release
  free (book);
}

Dynamic generation of an array of structures

This is a sample to dynamically generate array of structure.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// Definition of a structure that represents book information
struct myapp_book {
  int32_t id;
  const char * name;
  int32_t price;
};;

int main (void) {
  // Dynamic memory allocation for the structure array (calloc .0 is safe because it initializes)
  int32_t books_length = 10;
  struct myapp_book * books = calloc (sizeof (struct myapp_book), books_length);
  books [0] .id = 1;
  books [0] .name = "C99 Book";
  books [0] .price = 2000;
  
  printf("books [0] id:%d, name:%s, price:%d\n", books [0] .id, books [0] .name, books [0] .price);
  
  // Memory release
  free (books);
}

Associated Information