Created: 2023-08-21 12:50
Status: #concept
Subject: Programming
Tags: C Library Dynamic Memory Allocation

stdlib.h

It defines the NULL macro, size_t for Bytes, and other useful functions.

Process Control

Memory Allocation Functions

struct point { int x, y; } *p;

p = malloc(sizeof(struct point));
// initializes a 'point' struct with random values inside their members

p = calloc(1, sizeof(struct point));
// initializes a 'point' struct with 0 in all members
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   /* Deallocate allocated memory */
   free(str);
   
   return(0);
}

String Conversion Functions

See more string conversion functions here: WikiBooks Entry.

References