LINTOK

Section: C Library Functions (3)
Updated: 2 May 1989
Index Return to Main Contents
 

NAME

memlintok - package to stop lint complaints about memory allocation  

SYNOPSIS

#include "memlintok.h"  

DESCRIPTION

This package contains cpp macro definitions that stop lint from complaining about the memory management functions malloc(3), calloc(3), realloc(3), and free(3) (hereafter, we'll call these the "*alloc(3)" functions), while at the same time performing checks of their arguments. There are two sets of macros defined here: one without exception handling (like the *alloc(3) functions themselves) and one with.

The macros without exception handling are:

CALLOC_LINTOK(ptr, nelem, type)
MALLOC_LINTOK(ptr, nelem, type)
REALLOC_LINTOK(ptr, nelem, type)
FREE_LINTOK(ptr)

where "ptr" is a variable of type "type *". After invoking any of the "*ALLOC_LINTOK" macros, "ptr" will either be NULL (if and only if the underlying function returned it) or it will point to an array of "nelem" objects of type "type".

Note that the usage of MALLOC_LINTOK, CALLOC_LINTOK, and REALLOC_LINTOK requires more than a name change. An original call of the form:

ptr = (type *) malloc(nelem * sizeof(type))

must be replaced by

MALLOC_LINTOK(ptr, nelem, type)

and similarly for CALLOC_LINTOK and REALLOC_LINTOK. All three macros "return" values and may therefore be used as part of expressions.

If a macro's result is compared with NULL, lint will usually complain if NULL is cast to anything. For example, this will produce a lint error:

int *ia;

 ...
if (MALLOC_LINTOK(ia, 42, int) == (int *) NULL)

 ...

But since uncast 0 (i.e., NULL) is always a valid pointer, it is unnecessary to cast it to anything. Removing the cast stops the lint complaint:


 ...
if (MALLOC_LINTOK(ia, 42, int) == NULL)

 ...

If the macro invocation doesn't appear within an expression, you will need to cast the *ALLOC_LINTOK() invocations to void if you want to get rid of the "*alloc returns value which is sometimes ignored" and "*alloc returns value which is always ignored" messages.

You can mix invocations of these macros with calls to the original functions. The only difference is in the lint behavior.

A common way of invoking the memory management functions is to call malloc() if a pointer is NULL (usually the first time it's set) and realloc() if it isn't NULL (usually on subsequent times). The simple macro

MR_ALLOC_LINTOK(ptr, nelem, type)

does this with MALLOC_LINTOK() and REALLOC_LINTOK().

Since memory allocation failure is often an exceptional condition, "memlintok.h" also has definitions of four other useful macros that include exception handling:

CALLOC_OR_ELSE_LINTOK(ptr, nelem, type)
MALLOC_OR_ELSE_LINTOK(ptr, nelem, type)
REALLOC_OR_ELSE_LINTOK(ptr, nelem, type)
MR_ALLOC_OR_ELSE_LINTOK(ptr, nelem, type)

Each of these macros invokes the corresponding *ALLOC_LINTOK() macro and, if that macro fails, invokes another macro:

ERROR_EXIT_LINTOK(nelem, size)

Where "nelem" is the number of elements of "size" bytes attempting to be allocated.

A default ERROR_EXIT_LINTOK(nelem, size), which prints an informative message and dies, is also defined, but you are free to redefine it (after including "memlintok.h" and before invoking any of the macros herein -- remember to "#undef ERROR_EXIT_LINTOK" first) if you want to do your own error handling.  

SEE ALSO

malloc(3), stdio(3S)  

NOTE

"memlintok.h" assumes NULL is defined in <stdio.h> (which is usually the case).

It also assumes the cpp symbol "lint" is defined when and only when the code is being passed through lint and not generating real executable code.

These macros have been tested under SunOS, HP-UX, and Apollo DOMAIN/IX.  

BUGS

These macros are intended for use with the dynamic allocation of single elements of fixed size (i.e. structs or typedefs) or variably-sized arrays of same. In the author's experience, this covers most uses of the *alloc(3) functions. The macros do not work well, however, when creating variably-sized single structures that are not arrays.

At least one version of lint(1) (SunOS 4.0) complains when these macros are used with "void *" pointers. It needn't.

Apollo DOMAIN/IX lint(1) generates several bogus (I believe) complaints about the default exception handling macro.  

AUTHOR

Bob Lewis (bobl@tessi.uucp) produced these macros using his own resources and hereby releases them to the public domain. The author will not be held responsible for their use, misuse, abuse, or any damages arising from same.


 

Index

NAME
SYNOPSIS
DESCRIPTION
SEE ALSO
NOTE
BUGS
AUTHOR

This document was created by man2html, using the manual pages.
Time: 06:15:22 GMT, December 12, 2024