Malloc options via the environment

A useful feature: instead of programmatically using the mallopt(3) API, the system allows us to tune some allocation parameters conveniently via environment variables. Most useful, perhaps, from the viewpoint of debug and testing, the MALLOC_CHECK_ variable is the environment variable corresponding to the M_CHECK_ACTION parameter described earlier; thus, we can just set the value, run our application, and see the result for ourselves!

A few examples follow, using our usual membugs application to check out some test cases:

Test case # 10: double free with MALLOC_CHECK_ set:

$ MALLOC_CHECK_=1 ./membugs_dbg 10doublefree(): cond 0doublefree(): cond 1membugs.c:doublefree:134: malloc failed*** Error in `./membugs_dbg': ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.