linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Scott <drmemory@3rivers.net>
To: "Robert P. J. Day" <rpjday@mindspring.com>,
	linux-c-programming@vger.kernel.org
Subject: Re: how to implement routines that return general strings?
Date: Thu, 10 Aug 2006 11:12:38 -0600	[thread overview]
Message-ID: <20060810171238.GC778@drmemory.local> (raw)
In-Reply-To: <Pine.LNX.4.64.0608101004460.13097@localhost.localdomain>

On Thu, Aug 10, 2006 at 10:10:21AM -0400, Robert P. J. Day wrote:
> 
>   i'm about to design and implement a number of routines that return
> strings and, as far as i can tell, i have two general design choices.
> 
>   int		func(char* buf, int bufsiz) ;
>   char*		func() ;
> 

With the caveat that I haven't done any serious programming in C for
many years:  As I recall, I always used a protocol halfway between
these, as:

char *func(size_t bufsiz)
{
	char *retval = malloc (bufsiz);
	if (retval == NULL) return (NULL);
	/* whatever */
	return (retval);
}

My thinking being that the low-level memory allocation (or a static
buffer, if that is appropriate) is better handled in the lower-level
routine. NULL vs valid pointer was usually good enough, though I do
recall hacking something together with one of these to where a static
error-code was recorded in the routine if something went wrong, which
could be retrieved by sending a magic value for bufsiz:

char *foo = func(128);
if (foo == NULL) {
  errcode = (int) func(0xFFFF);
  .....
}

I suppose violating all precepts of proper coding, but it used to
work.....

Good luck, and I'd appreciate hearing what your final decision is!

Scott Swanson

  reply	other threads:[~2006-08-10 17:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-10 14:10 how to implement routines that return general strings? Robert P. J. Day
2006-08-10 17:12 ` Scott [this message]
2006-08-10 19:04   ` David Rientjes
2006-08-11 20:06   ` Kelly Burkhart
2006-08-12  9:36     ` Robert P. J. Day
2006-08-14 18:15       ` Scott
2006-08-10 21:07 ` Glynn Clements
2006-08-11  7:03   ` Raseel Bhagat
2006-08-11 18:36     ` Shorty Porty
  -- strict thread matches above, loose matches on Subject: below --
2006-08-10 16:51 Khan, Ahmer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060810171238.GC778@drmemory.local \
    --to=drmemory@3rivers.net \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=rpjday@mindspring.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).