.\" Part of publib. .\" .\" Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" .\" 2. Redistributions in binary form must reproduce the above .\" copyright notice, this list of conditions and the following .\" disclaimer in the documentation and/or other materials provided .\" with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS .\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING .\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .\" part of publib .\" "@(#)publib-alloc:$Id: xmalloc.3,v 1.5 1998/10/25 20:48:38 liw Exp $" .\" .TH XMALLOC 3pub "C Programmer's Manual" "Publib" "C Programmer's Manual" .SH NAME xmalloc, xrealloc, xfree, xstrdup, xmemdup, memdup \- memory allocation functions for Publib .SH SYNOPSIS #include .sp 1 .nf void *\fBxmalloc\fR(size_t \fIbytes\fR); void *\fBxrealloc\fR(void *\fIptr\fR, size_t \fIbytes\fR); void \fBxfree\fR(void *\fIptr\fR); char *\fBxstrdup\fR(const char *\fIstring\fR); void *\fBmemdup\fR(const void *\fImem\fR, size_t \fIbytes\fR); void *\fBxmemdup\fR(const void *\fImem\fR, size_t \fIbytes\fR); .SH "DESCRIPTION" These functions are utility functions for memory allocation from the publib library. \fIxmalloc\fR, \fIxrealloc\fR, and \fIxfree\fR are error checking versions of the standard library routines \fImalloc\fR, \fIrealloc\fR, and \fIfree\fR, respectively. They are guaranteed to never return unless there was no problem: if, for example, \fIxmalloc\fR is unable to allocate the requested amount of memory, it prints an error message and terminates the program. Hence, the caller does not need to check for a NULL return value, and the code that calls these functions is simpler due to the lack of error checks. .PP Similarly, \fIxstrdup\fR is an error checking version of the common (though not standard) \fIstrdup\fR routine, which creates a duplicate of a string by allocating memory for the copy with \fImalloc\fR. (For systems that lack \fIstrdup\fR, publib provides one in its portability module; it is always declared in .) .PP \fImemdup\fR is similar to \fIstrdup\fR, it creates a copy of an arbitrary memory area (the arguments are a pointer to the beginning of the area, and its size) by allocating memory for the copy with \fImalloc\fR. \fIxmemdup\fR is its error checking version. .SH NOTE \fIxmalloc\fR and \fIxrealloc\fR treat a request to allocate a block of 0 bytes as an error. \fIxrealloc\fR will allow its first argument to be NULL. .SH "SEE ALSO" publib(3), malloc(3), strdup(3) .SH AUTHOR Lars Wirzenius (lars.wirzenius@helsinki.fi)