Dynamic Memory
Dynamic memory allocation, through the use of functions like malloc()
and realloc()
, is a powerful tool that allows programmers to manipulate memory and variable storage. In this section, we go over how to use malloc()
, and its counterpart free()
, in relation to using pointers, as well as some good practices when allocating memory. Additionally, a few common mistakes are explained, many of which can be found using Valgrind.
-
Lecture
-
Short
-
Supplementary Resources
- Man pages on
malloc()
,realloc()
,calloc()
, andfree()
- Tutorials Point on C library function - malloc
- Microsoft Developer Network on malloc
- Man pages on
-
Thought Questions
- What does dynamic memory allocation allow us to do?
- How can we translate
malloc()
andfree()
into pseudocode? - Should we always have an equal number of calls to
malloc()
andfree()
?