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

Wrapping, I am confused

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


P: n/a
da***********@yahoo.com wrote:
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2

P: n/a
On 29 Sep 2003 06:07:35 -0700
da***********@yahoo.com wrote:
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;
}
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.