From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Freeing pointers

Freeing pointers - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Freeing pointers

- [Instructor] The free function unallocates memory, but I also recommend that you assign the memory's pointer to the null constant after it's freed which serves as a safeguard. So in this code, the hello function accepts and outputs a string of text. The input pointer is allocated a chunk of memory at line nine. The function doesn't free the memory, though the pointer's value, the address, is lost once the function is done. Therefore the allocated memory chunk just lingers in memory. In this improvement to the code, the allocated memory is freed at line 25. By doing so, the memory is available for allocation elsewhere, especially in code where a function allocates a huge chunk of memory and the buffer is used only within a function, always release it before the function terminates. This code is similar to the preceding example, though all the action takes place in the main function. At line 26, the memory allocated is freed.…

Contents