Day 10-18 - Malloc Package malloc = memory allocation...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Malloc Package malloc = memory allocation #include <stdlib.h> Void *malloc(size_t size) If successful: o Returns a pointer to a memory block of at least size bytes, (typically) aligned to 8- byte boundary o If size == 0; return NULL If unsuccessful: returns NULL (0) and sets errno Void free(void *p) Returns the block pointed at by p to pool of available memory P must come from a previous call to calloc or realloc Void *realloc(void *p, size_t size) Changes size of block p and returns pointer to new block Contents of new block unchanged up to min of old and new size Assumptions Assumptions made in this lecture o Memory is word addressed (each word can hold a pointer) One Block Is One Word And if Shade d It’s a allocate d word Otherwise it is a free word Allocation examples P1 = malloc(4) Constraints Applications: o Can issue arbitrary sequence of allocation and free requests o Free requests must correspond to an allocated block Allocators o Cant control number or size of allocated blocks
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

Day 10-18 - Malloc Package malloc = memory allocation...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online