This video provides a description and example of malloc(), free(), and realloc().
- [Instructor] Pointers simply point to variables. Well more specifically the R variables that hold memory addresses, so creating a pointer is by no means a guarantee that it will point to valid data. A pointer may point to existing local or global variables. But you may also want to create new variables dynamically and assign their addresses to pointers. These new variables need some exclusive space in volatile memory and that's what a memory allocation function does.
It reserves some space in memory for a new piece of data and this space will not be used for anything else while this space is reserved. Stdlib.h provides several memory allocation functions. Let me show you three. We have malloc, realloc, and free. Malloc allocates as many types as specified by its argument and it returns the address of this newly allocated space. Realloc resizes a previously allocated space pointed to by its first argument.
The second argument is its new size. Because this may require additional space in an already packed memory, the whole data segment may be relocated so the original address of the pointer is not guaranteed to remain unchanged. The new address is returned. Finally the free function deallocates the space pointed by its argument, leaving it free for further allocations.
- Working with header files
- C constants and types
- Unformatted and formatted input/output
- Accessing files with C functions
- Generating random numbers
- Converting strings
- Sorting and searching for data
- Manipulating strings
- Using mathematical functions
Skill Level Intermediate
1. The C Standard Library
2. Input and Output Library: stdio.h
3. General Purpose: stdlib.h
4. String Functions: string.h
5. Mathematical Functions: math.h
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.