How to use #define DEBUG to run the malloc function in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DEBUG

void* debug_malloc(size_t size);

#define MALLOC(n) debug_malloc(n) 

int main() {
	#ifdef DEBUG
		char *p = MALLOC(64);
		strcpy(p, "C Programming");
		puts(p);
		free(p);
	#endif

	return 0;
}

void* debug_malloc(size_t size) {
	printf("debug_malloc - file: %s line: %d\n", __FILE__, __LINE__ + 1);
	return (char*)malloc(size * sizeof(char));
}



/*
run:

debug_malloc - file: C:\Examples\src\main.c line: 28
C Programming

*/

 



answered Feb 9, 2023 by avibootz
edited Feb 9, 2023 by avibootz

Related questions

2 answers 136 views
136 views asked Feb 9, 2023 by avibootz
1 answer 176 views
1 answer 173 views
1 answer 184 views
1 answer 191 views
1 answer 164 views
...