Q:

How write a replacement function for a standard C library function

I have a school assignment that is calling for the replacement of the standard memory allocation functions within the standard library of C. I am not sure how to do this but here is the brief description my teacher is using to outline his process. 

Next, I'm going to #define the standard C memory allocation and free
  functions to be replaced by their memcheck counterparts.  In each
  case, I'm looking up the number and names of the arguments in the man
  page and using them in both the original function and its
  redefinition.  In addition, to the arguments that are listed in the
  man page, I am also passing (underscore, underscore)FILE(underscore, underscore) and LINE(underscore, underscore) to the replacement
  versions as the last two arguments of each function.  I am being
  careful not to include a semi-colon on either of the two functions in
  the #define statement.

right now I am just working on the header file: 
#ifndef MEMCHECK_H
#define MEMCHECK_H

#include<stdlib.h>

#define malloc _memcheck_malloc
#define free _memcheck_free

#endif

The point of this project is to replace the standard malloc and free functions with one that is our own and that is being utilised within a linked list. 
ultimately what i am asking for is "How do i begin defining my own function that will replace a standard function?" I need to used the #define method as well as a few compiler marcos stated above. 
Here is a link to another question that i have found that appears to be the right answer but i am not sure. 
How to replace C standard library function ? (3rd answer).

A:

The __FILE__ macro expands to a const char* containing the name of the current source file. The __LINE__ macro expands to an int that is the line number in the current file where the macro is located. So, the declaration of your new function would have to look like this:
void *_memcheck_malloc(size_t size, const char *file, int line);

When you define a macro to change all calls to malloc to instead call _memcheck_malloc, you have to define the additional parameters that the macro will automatically supply to the different function, because any existing call to malloc won't include them. You want this
32    int *p = malloc(4 * sizeof(int));

to be changed into this:
32    int *p = _memcheck_malloc(4 * sizeof(int), "filename.c", 32);

So, your macro definition looks like this:
#define malloc(X) _memcheck_malloc((X), __FILE__, __LINE__)

The X argument is replaced with whatever exists in the original text; this is simple pattern-matching, so everything within the parentheses in the original text is replaced with everything within the parentheses in the macro invocation. The parentheses around the X argument (in the replacement text) are to make sure that whatever expression is supplied to the macro is evaluated as a single value.
The free(), calloc(), and realloc() macros would follow this pattern.
CAVEAT Macros can be just as tricky as hell, and in the main, should be avoided if possible. This situation is one of the few widely accepted uses of macro substitution. Don't be sucked into the idea that "Wow, macros are cool -- I can do all kinds of things with this!" You can, and if you do, you will almost invariably come to regret it.

