malloc function --Dynamic allocation of memory

Memory can be allocated dynamically by using the malloc function. You need to load "stdlib.h".

#include <stdlib.h>

void * malloc (size_t size);

The malloc function allocates memory in the heap area.

The first argument specifies the size to allocate in bytes. The type is 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.

Note that the memory allocated by the malloc function has not been initialized.

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

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

sample malloc function

Let's write a sample of the malloc 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 = malloc (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 (malloc)
  struct myapp_book * book = malloc (sizeof (struct myapp_book));
  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 structure array
  int32_t books_length = 10;
  struct myapp_book * books = malloc (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