Wrapping, I am confused

D

dam_fool_2003

My understanding about wrapping is that we add utilities to a lib
function for our specific use. So days ago in the c.l.c I saw a
function def as:

void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
error ("virtual memory exhausted");
return value;
}

The above is useful for mem allocation and checking weather the malloc
is a success.

Is it the wrapping is all about?

(I could be funny by my understanding but pls help)
 
M

Mark Gordon

On 29 Sep 2003 06:07:35 -0700
My understanding about wrapping is that we add utilities to a lib
function for our specific use. So days ago in the c.l.c I saw a
function def as:

void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
error ("virtual memory exhausted");
return value;
}

The above is useful for mem allocation and checking weather the malloc
is a success.

Is it the wrapping is all about?

Yes, that is one of the uses of wrapping. It can also be used to extend
or provide slight modifications to the behaviour of functions, such as
making realloc well defined when passed a size of 0 with, for example,

void * myrealloc(void *ptr, size_t size)
{
if (size) return realloc(ptr, size);
free(ptr);
return NULL;
}
 
J

Jens.Toerring

My understanding about wrapping is that we add utilities to a lib
function for our specific use. So days ago in the c.l.c I saw a
function def as:
void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
error ("virtual memory exhausted");
return value;
}
The above is useful for mem allocation and checking weather the malloc
is a success.
Is it the wrapping is all about?

You don't call malloc() here directly but through xmalloc(), so you
"wrapped" the malloc() function by adding another layer between your
program and the call of malloc(). The advantage is that you don't have
to check after each call of malloc() that it did succeed (by comparing
the return value to NULL) but when xmalloc() returns you can be sure
it succeeded - otherwise error() would have been called, killing the
the program (at least I hope that's what error() is doing).

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,354
Latest member
OrenKrause

Latest Threads

Top