Computer Science 2211A/B Lecture Notes - Lecture 17: Null Pointer, C Dynamic Memory Allocation

92 views1 pages

Document Summary

Ability to allocate storage during program execution. Simple terms - can grow/shrink structures when needed. Malloc : allocates a block of memory but no need to initialize it. Returns a value of type void * (generic pointer) calloc : allocates a block of memory and clears it realloc : resizes a previously allocated block of memory. If memory allocation function cannot locate a block of memory, returns a null pointer. After function returns value in a pointer, we need to test if it is a null pointer eg. p = malloc(1000); if (p == null) { /* deal with it */ } Allocates a block of size bytes and returns a pointer to it. Add 1 to strings for null character strcpy(p abc ); // initialize array if p is pointing to an array calloc. Allocates space for an array with nmemb elements, each size bytes long. Returns null pointer if not enough space.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents