John Mavrick's Garden

Search IconIcon to open search

Last updated Unknown

Status: Tags: Links: C MOC


C Memory Allocation

Practices

Functions

realloc

Expands or contracts the existing area pointed to by ptr, copying to new location

1
void* realloc(void *ptr, size_t size) 

memcpy

1
2
void* memcpy(void * dest, const void* source, size_t num) 
// copies the values of num bytes from a source to dest

memset

1
2
void * memset(void * ptr, int val, size_t num) 
// sets the first num bytes in memory to be val

calloc

1
2
3
4
5
6
7
void * calloc(int num_of_items, int size) 
// allocates num_of_items objects of the specified size
// in the memory and sets the memory to 0 
// essentially equivalent to 

void * ptr = malloc(num_of_items*size);
memset(ptr, 0, num_of_items*size); 

Syntax

1
2
3
4
5
6
7
(void *) malloc(size_t) 
	// allocates size_t consecutive bytes in memory
	// size_t alias for unsigned integer type 

// ======= Usage =======

int * array = (int*) malloc(n * sizeof(int));

Backlinks

1
list from C Memory Allocation AND !outgoing(C Memory Allocation)

References:

Created:: 2021-09-27 15:40


Interactive Graph