mcheck, mcheck_check_all, mcheck_pedantic, mprobe - heap consistency checking
#include <mcheck.h> int mcheck(void (*abortfunc)(enum mcheck_status mstatus)); int mcheck_pedantic(void (*abortfunc)(enum mcheck_status mstatus)); void mcheck_check_all(void); enum mcheck_status mprobe(void *ptr);
The mcheck() function installs a set of debugging hooks for the malloc(3) family of memory-allocation functions. These hooks cause certain consistency checks to be performed on the state of the heap. The checks can detect application errors such as freeing a block of memory more than once or corrupting the bookkeeping data structures that immediately precede a block of allocated memory. To be effective, the mcheck() function must be called before the first call to malloc(3) or a related function. In cases where this is difficult to ensure, linking the program with -lmcheck inserts an implicit call to mcheck() (with a NULL argument) before the first call to a memory-allocation function. The mcheck_pedantic() function is similar to mcheck(), but performs checks on all allocated blocks whenever one of the memory-allocation functions is called. This can be very slow! The mcheck_check_all() function causes an immediate check on all allocated blocks. This call is effective only if mcheck() is called beforehand. If the system detects an inconsistency in the heap, the caller-supplied function pointed to by abortfunc is invoked with a single argument, mstatus, that indicates what type of inconsistency was detected. If abortfunc is NULL, a default function prints an error message on stderr and calls abort(3). The mprobe() function performs a consistency check on the block of allocated memory pointed to by ptr. The mcheck() function should be called beforehand (otherwise mprobe() returns MCHECK_DISABLED). The following list describes the values returned by mprobe() or passed as the mstatus argument when abortfunc is invoked: MCHECK_DISABLED (mprobe() only) mcheck() was not called before the first memory allocation function was called. Consistency checking is not possible. MCHECK_OK (mprobe() only) No inconsistency detected. MCHECK_HEAD Memory preceding an allocated block was clobbered. MCHECK_TAIL Memory following an allocated block was clobbered. MCHECK_FREE A block of memory was freed twice.
mcheck() and mcheck_pedantic() return 0 on success, or -1 on error.
The mcheck_pedantic() and mcheck_check_all() functions are available since glibc 2.2. The mcheck() and mprobe() functions are present since at least glibc 2.0
For an explanation of the terms used in this section, see attributes(7). Interface Attribute Value mcheck(), mcheck_pedantic(), Thread safety MT-Unsafe race:mcheck mcheck_check_all(), mprobe() const:malloc_hooks
These functions are GNU extensions.
Linking a program with -lmcheck and using the MALLOC_CHECK_ environment variable (described in mallopt(3)) cause the same kinds of errors to be detected. But, using MALLOC_CHECK_ does not require the application to be relinked.
The program below calls mcheck() with a NULL argument and then frees the same block of memory twice. The following shell session demonstrates what happens when running the program: $ ./a.out About to free About to free a second time block freed twice Aborted (core dumped) Program source #include <stdlib.h> #include <stdio.h> #include <mcheck.h> int main(int argc, char *argv[]) { char *p; if (mcheck(NULL) != 0) { fprintf(stderr, "mcheck() failed\n"); exit(EXIT_FAILURE); } p = malloc(1000); fprintf(stderr, "About to free\n"); free(p); fprintf(stderr, "\nAbout to free a second time\n"); free(p); exit(EXIT_SUCCESS); }
malloc(3), mallopt(3), mtrace(3)
This page is part of release 4.09 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.
Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.
Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.
Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.
Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.
The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.
Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.
Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.
Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.