Common Errors
Learn to avoid common pitfalls when dealing with dynamic memory allocations.
We'll cover the following
Memory leaks
We’ve already discussed memory leaks quite extensively. However, this is such an important concept that we’ll reiterate it. Pretty much everyone can write code with dynamic memory allocations, but not everyone can write it correctly.
Let’s look at the case of strdup
and investigate a memory leak. In the following code, we duplicate a string using strdup
, but we never free the memory.
Recall that strdup
dynamically allocates a copy and returns it to the caller, which is responsible for freeing the memory.
Get hands-on with 1400+ tech skills courses.