mirror of
https://github.com/envmodules/modules.git
synced 2026-06-18 00:06:53 +08:00
145 lines
4.2 KiB
Groff
145 lines
4.2 KiB
Groff
.\" RCSID @(#)$Id: memdebug.man,v 1.1 2002/08/27 21:07:41 rkowen Exp $
|
|
.\" LIBDIR
|
|
.TH "MEMDEBUG" "3rko" "12 May 1995"
|
|
.SH NAME
|
|
memdebug \- a set of macros that provides a front-end for the memory allocation
|
|
routines to help find memory leaks. Supports only the ANSI-C routines:
|
|
.IR calloc , free , malloc , realloc.
|
|
|
|
.SH SYNOPSIS
|
|
|
|
\.\.\. usual standard headers
|
|
#define MEMDEBUG
|
|
#include "memdebug.h"
|
|
\.\.\. code that calls the memory allocation routines
|
|
|
|
.SH DESCRIPTION
|
|
.I memdebug
|
|
provides several front-end routines for the ANSI-C memory allocation
|
|
routines. It outputs the routine name, pointer(s), file name,
|
|
and line number to output. This can be analyzed to see that allocations
|
|
are matched with deallocations.
|
|
|
|
.P
|
|
The only user callable function is
|
|
.I FILE *memdebug_output(FILE *output)
|
|
which will set the output stream for the memdebug routines
|
|
if output is not NULL, else it will return the FILE *stream
|
|
that will be used if output is set NULL. The default stream
|
|
is
|
|
.IR stderr .
|
|
This routine should be called before any memory allocations are done.
|
|
.P
|
|
This routine is provide, because it may be more convenient to set it to
|
|
.I stdout
|
|
or whatever file stream the regular output goes to
|
|
so that the MEMDEBUG lines can be
|
|
synchronized between the program and the memory
|
|
allocation calls.
|
|
|
|
.SH EXAMPLE
|
|
.nf
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#define MEMDEBUG
|
|
#include "memdebug.h" /* include this AFTER stdlib.h */
|
|
|
|
typedef struct Trial {
|
|
int a;
|
|
double b;
|
|
char c[5];
|
|
struct Trial *next;
|
|
} trial;
|
|
|
|
int main() {
|
|
|
|
size_t num = 20;
|
|
int *ia = (int *) NULL;
|
|
double *da = (double *) NULL;
|
|
char *ca = (char *) NULL;
|
|
trial *ta = (trial *) NULL;
|
|
|
|
printf("Some regular output\n");
|
|
memdebug_output(stdout); /* set memdebug output */
|
|
|
|
if ((ia = (int *) malloc(num * sizeof(int))) == NULL)
|
|
(void) fprintf(stderr,"malloc error\n");
|
|
free(ia);
|
|
ia = (int *) NULL;
|
|
|
|
if ((ia = (int *) malloc(2 * num * sizeof(int))) == NULL)
|
|
(void) fprintf(stderr,"malloc error\n");
|
|
if ((ia = (int *) realloc(ia, 4 * num * sizeof(int))) == NULL)
|
|
(void) fprintf(stderr,"realloc error\n");
|
|
|
|
if ((da = (double *) malloc(num * sizeof(double))) == NULL)
|
|
(void) fprintf(stderr,"malloc error\n");
|
|
|
|
if ((ta = (trial *) calloc(num, sizeof(trial))) == NULL)
|
|
(void) fprintf(stderr,"malloc error\n");
|
|
free(ta);
|
|
ta = (trial *) NULL;
|
|
|
|
if ((ta = (trial *) calloc(2 * num, sizeof(trial))) == NULL)
|
|
(void) fprintf(stderr,"malloc error\n");
|
|
|
|
if ((da = (double *) realloc(da, 2 * num * sizeof(double))) == NULL)
|
|
(void) fprintf(stderr,"realloc error\n");
|
|
free(ia);
|
|
ia = (int *) NULL;
|
|
|
|
if ((ca = (char *) malloc(num * sizeof(char))) == NULL)
|
|
(void) fprintf(stderr,"malloc error\n");
|
|
printf("Some more regular output\n");
|
|
|
|
free(da);
|
|
free(ta);
|
|
free(ca);
|
|
free(ia);
|
|
|
|
return 0;
|
|
}
|
|
.fi
|
|
.P
|
|
The output generated by the above code should look something like:
|
|
.nf
|
|
Some regular output
|
|
RKOMEM: malloc : tmemdebug.c 36 : 0x8049fd8 80
|
|
RKOMEM: free : tmemdebug.c 38 : 0x8049fd8
|
|
RKOMEM: malloc : tmemdebug.c 41 : 0x8049fd8 160
|
|
RKOMEM: realloc_free : tmemdebug.c 43 : 0x8049fd8
|
|
RKOMEM: realloc_malloc : tmemdebug.c 43 : 0x8049fd8 320
|
|
RKOMEM: malloc : tmemdebug.c 46 : 0x804a120 160
|
|
RKOMEM: calloc : tmemdebug.c 49 : 0x804a1c8 20 24
|
|
RKOMEM: free : tmemdebug.c 51 : 0x804a1c8
|
|
RKOMEM: calloc : tmemdebug.c 54 : 0x804a1c8 40 24
|
|
RKOMEM: realloc_free : tmemdebug.c 57 : 0x804a120
|
|
RKOMEM: realloc_malloc : tmemdebug.c 57 : 0x804a590 320
|
|
RKOMEM: free : tmemdebug.c 59 : 0x8049fd8
|
|
RKOMEM: malloc : tmemdebug.c 62 : 0x8049fd8 20
|
|
Some more regular output
|
|
RKOMEM: free : tmemdebug.c 67 : 0x804a590
|
|
RKOMEM: free : tmemdebug.c 68 : 0x804a1c8
|
|
RKOMEM: free : tmemdebug.c 69 : 0x8049fd8
|
|
RKOMEM: free : tmemdebug.c 70 : (null)
|
|
.fi
|
|
|
|
.SH SEE ALSO
|
|
.\" crontab(1),stat(2),libmon(8)
|
|
memdbg(1rko), calloc(3), free(3), malloc(3), realloc(3)
|
|
|
|
.SH NOTES
|
|
Must define the macro MEMDEBUG before include the memdebug.h header.
|
|
This header should be included after the standard headers, particularly
|
|
stdlib.h which may be included in some of the other standard headers also.
|
|
|
|
If the macro MEMDEBUG is not defined
|
|
then nothing happens and the standard memory
|
|
allocation calls are used.
|
|
|
|
.SH AUTHOR
|
|
R.K.Owen,Ph.D.
|
|
|
|
.KEY WORDS
|