🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2441

Re: void* type

#2441
From: Stan Ryckman
Date: Sun, 31 Mar 1996 16:43
57 lines
2241 bytes
In article <4jibl5$89u@solutions.solon.com>,
J. Kanze <kanze@gabi-soft.fr> wrote:
>In article <4jckqc$a5o@solutions.solon.com> watzka@stat.uni-muenchen.de
>(Kurt Watzka) writes:

[snip]

>|>    SomeType *a, b;
>
>|>    if (!swap(&a, &b))
>|>       /* die whining */
>
>And you forgot your own extra parameter:-):

And you forgot one ")" in:
>
>    if ( ! swap( &a , &b , sizeof( SomeType ) )
>        ...
Or perhaps you moved it up to your smiley  :)

And everyone's missed that interesting declaration for a and b
so far.  Reminds me of a recent thread  :)

>
>BTW, I've found that a small optimizing trick in functions like this can
>sometimes work wonders for performance:
>
>    int
>    swap( void* a , void* b , size_t sz )
>    {
>        unsigned char   buffer[ EXPECTEDMAXSIZE ] ;
>        void*           tmp = (sz <= sizeof( buffer ))
>                                ? buffer
>                                : malloc( sz ) ;

Or, if you don't need reentrant or resursive code, you can do:
	static void *tmp = NULL;
	static size_t tmp_sz = 0;

	if (sz > tmp_sz) {
	    .... use realloc here .... you get the idea ....

Details left to the reader (I'm feeling lazy tonight  :)

This avoids having to guess what EXPECTEDMAXSIZE might be,
but also suffers from never freeing that buffer.  OTOH, it might
end up avoiding zillions of calls to malloc if the guess was poor.
I've had a couple of thing like that bite when data formats
were changed, and I had code inherited from people long-gone.

The realloc can certainly do something like use sz+10000
to "grow" the tmp area in tunable chunks, depending on the application.

Cheers,
Stan.
--
Stan Ryckman  (stanr@tiac.net)
Technical Details
Message-ID: <4jmcnr$lr2@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4iepre$ag7@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> <4jckqc$a5o@solutions.solon.com> <4jibl5$89u@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: stanr@tiac.net (Stan Ryckman)
Newsgroups: comp.lang.c.moderated
Subject: Re: void* type
Date: 31 Mar 1996 10:43:39 -0600
Organization: Amber & Sneakers Fan Club
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4jmcnr$lr2@solutions.solon.com>
References: <4iepre$ag7@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> <4jckqc$a5o@solutions.solon.com> <4jibl5$89u@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com