S
Spry
Hi,
I wanted to write macros for finding the number of memory allocations and
deallocations also wanted to find the locations.
The code I have is a pretty big one. I have a wrapper on top of malloc(int
x) as Xmalloc(int x)
Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.
I cannot use function I need to use #define since I want to use __FILE__ and
__LINE__ if I use a function then it will give the file name and the line of
that funciton.
File: main.c
--------------
#include <stdio.h>
#include "my_macro.h"
main()
{
int *p;
int i = 10;
p=Xmalloc(i);
Xfree(p);
}
File: Xalloc.h
------------------
void * Xmalloc(int x)
{
return (void *)(malloc(x));
}
void Xfree(void *p)
{
free(p);
}
File: my_macro.h
-------------------
#include "Xalloc.h"
//#define Xmalloc(x) Xmalloc(x)
#define Xfree(p) printf("FILE: %s LINE: %d Pointer:
%u",__FILE__,__LINE__,p );Xfree(p)
The problem I am facing is to write the macro for Xmalloc in the file
my_macro.h which allocates the memory and then logs the __FILE__ __LINE__
, the pointer value and the number of bytes.
Could some one please help in coming up with such an macro.
Regards,
Spry.
I wanted to write macros for finding the number of memory allocations and
deallocations also wanted to find the locations.
The code I have is a pretty big one. I have a wrapper on top of malloc(int
x) as Xmalloc(int x)
Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.
I cannot use function I need to use #define since I want to use __FILE__ and
__LINE__ if I use a function then it will give the file name and the line of
that funciton.
File: main.c
--------------
#include <stdio.h>
#include "my_macro.h"
main()
{
int *p;
int i = 10;
p=Xmalloc(i);
Xfree(p);
}
File: Xalloc.h
------------------
void * Xmalloc(int x)
{
return (void *)(malloc(x));
}
void Xfree(void *p)
{
free(p);
}
File: my_macro.h
-------------------
#include "Xalloc.h"
//#define Xmalloc(x) Xmalloc(x)
#define Xfree(p) printf("FILE: %s LINE: %d Pointer:
%u",__FILE__,__LINE__,p );Xfree(p)
The problem I am facing is to write the macro for Xmalloc in the file
my_macro.h which allocates the memory and then logs the __FILE__ __LINE__
, the pointer value and the number of bytes.
Could some one please help in coming up with such an macro.
Regards,
Spry.