By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Macro for memory logging using __FILE__ and __LINE__

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Spry wrote:
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.


You can find code to do precisely this in the Library With No Name, which is
downloadable from http://www.rjgh.co.uk/prg/c/wnn/index.php#Download

To save you the effort, though, here's a quote from the header:

/*! You can control whether wnn_Malloc, etc, call the standard library
routines or the CLINT wrappers by defining, or not defining, the
WNN_USE_STDALLOC macro.
*/
#ifdef WNN_USE_STDALLOC
/*! You have elected to define WNN_USE_STDALLOC, so the standard
library calls will be invoked directly.
*/
#define wnn_Malloc(wnn_SIZE) malloc(wnn_SIZE)
#define wnn_Calloc(wnn_COUNT, wnn_SIZE) calloc(wnn_COUNT, wnn_SIZE)
#define wnn_Realloc(wnn_OLD, wnn_NEW) realloc(wnn_OLD, wnn_NEW)
#define wnn_Free(wnn_OLD) free(wnn_OLD)
#else
/*! You have elected not to define WNN_USE_STDALLOC, so the standard
library calls will be wrapped by CLINT calls.
*/
#define wnn_Malloc(wnn_SIZE) wnn_malloc(wnn_SIZE, __FILE__, __LINE__)
#define wnn_Calloc(wnn_COUNT, wnn_SIZE) \
wnn_calloc(wnn_COUNT, wnn_SIZE, __FILE__, __LINE__)
#define wnn_Realloc(wnn_OLD, wnn_NEW) \
wnn_realloc(wnn_OLD, wnn_NEW, __FILE__, __LINE__)
#define wnn_Free(wnn_OLD) wnn_free(wnn_OLD, __FILE__, __LINE__)
#endif

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.