1

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).

  • 1
    If you think it holds the answer - why bother to write another Q for that? If you do not think, explain why not. – Patrick Artner Mar 04 '18 at 16:53
  • @PatrickArtner I have no idea if this answer is correct, and a straight forward answer has been hard to find on google. I am not sure if it is correct solely because i do not entirely understand how #define can be used to replace standard functions. – Tyler Green Mar 04 '18 at 17:00
  • The `#define` doesn't change the library, but modifies any *calls* to the standard function into calling your function instead. The preprocessor replaces `free(ptr)` with `_memcheck_free(ptr)` and the compiler doesn't even know it did that. – Bo Persson Mar 04 '18 at 17:11
  • 2
    @BoPersson So by the first argument of #define being "malloc" and the second argument being "The name of the function i am replacing malloc with" every time malloc is being called, it will used my replacement function? so I all I need to do now is declare a prototype for the replacement function within my header file and then create a function file that hold the definition of my replacement file. is that correct? – Tyler Green Mar 04 '18 at 17:17

1 Answers1

3

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.

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31