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.

Using return values

Using return values - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Using return values

- [Instructor] Many functions return values, which are often discarded, either out of expediency, or because the programmer assumes everything went well. Always check return values. For example, in this code, an if test at line nine confirms that the malloc function allocated memory. Even for a tiny quantity such as 32 bytes, always test this return value. Ditto for the F open function. Its return value is tested at line nine in this code. Never assume the file point returned is valid. Always test it against the null constant. Further, the F close function returns a value, which is tested here at line 17. It's possible to process an error condition, and determine why the file fails to close, but only when you retain that returned value. Functions that write data to a file return the number of bytes or characters written. This return value must also be checked to confirm that the write operation was successful, which…

Contents