Created: 2023-08-21 12:50
Status: #concept
Subject: Programming
Tags: C Library Dynamic Memory Allocation
stdlib.h
Process Control
void exit(int signal)
- Allows us to
return
from theint main()
function of the program anywhere in our code. EXIT_SUCCESS
andEXIT_FAILURE
are Macro Definitions bound to0
and1
respectively.
- Allows us to
int system(const char *cmd)
Memory Allocation Functions
void *malloc(size_t bytes)
- allocates
bytes
of memory and returns avoid *
which can be type casted into other type Pointer Variables.
- allocates
void *calloc(size_t items, size_t bytes)
- allocates
items * bytes
of memory likemalloc()
, but sets all the bits in the byte to0
, clearing any data in the Memory Addresses.
- allocates
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
void free(void *ptr)
- frees allocated memory pointed by
ptr
.
- frees allocated memory pointed by
#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.