69,373
社区成员
发帖
与我相关
我的任务
分享
/**
* @file stdlib.h
* @author mymtom
*/
#ifndef STDLIB_H
#define STDLIB_H
#include_next <stdlib.h>
#include <stdio.h>
static void * my_malloc(const char *file, int line, const char *func, size_t size)
{
void *ptr;
ptr = malloc(size);
/* 记录调用函数名称和返回值 */
printf("位于文件%s第%d行的函数%s调用了malloc, size=%d, 返回值为%p\n", file, line, func, size, ptr);
return ptr;
}
#define malloc(size) my_malloc(__FILE__, __LINE__, __FUNCTION__, size)
#endif /* STDLIB_H */
/**
* @file main.c
* @author mymtom
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
malloc(80);
return 0;
}
注意自己的stdlib.h位于当前目录下,所以要加上-I.
$ gcc -I. main.c -o main
$ ./main
位于文件main.c第11行的函数main调用了malloc, size=80, 返回值为0x28201060