home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!fuug!demos!kiae!glas!demos!oracle.us!us.oracle.com!wkaufman
- From: wkaufman@us.oracle.com
- Newsgroups: comp.lang.c
- Date: 17 Jul 92 23:52 MDT
- Subject: Re: (Help) dynamic use of sprintf ?
- Sender: Notesfile to Usenet Gateway <notes@glas.apc.org>
- Message-ID: <1992Jul17.195250.16522@oracle.us>
- References: <brself.711345285@hal>
- Nf-ID: #R:brself.711345285@hal:945470687:1992Jul17.195250.16522@oracle.us:1418735349:001:1925
- Nf-From: us.oracle.com!wkaufman Jul 17 23:52:00 1992
- Lines: 45
-
-
- In article <brself.711345285@hal> brself@hal.gnu.ai.mit.edu (Ben Self) writes:
- ]
- ] ...problems w/sprintf and buffer sizing...]
- ]
- ] I have tried many methods in the past, but am not happy with any yet tried.
- ]
- ] Some of these include:
- ]
- ] . using the return value of fprintf () to a file pointer attached
- ] to /dev/null with the same arg format that I pass to sprintf ()
-
- Oooh, icchh! All the overhead of opening a useless file, writing to
- the file every time, closing the file; the malloc()'s, free()'s and
- system calls the stdio functions use. This solution I'd *absolutely*
- argue against.
-
- ] . determining the size of integers by log10 or (in the event I
- ] don't want to link to the math library) dividing by 10 until
- ] it becomes fractional.
-
- It should be sufficient to add up the *maximum* sizes of all the
- objects: for example, ints will be, at most, log10(INT_MAX) long. On my
- machine, that's about 11; so, at most, I'm only wasting about 10
- bytes--certainly a fair price to pay if I avoid doing log10()'s.
-
- ] . writing my own subset of sprintf () which takes care of this
- ] problem [far more work than it is worth].
-
- That's actually not a bad idea, if you're only using a small subset
- of the sprintf() options. For example, writing one that takes only "%s"
- and "%d" would be easy (well, as easy as any variadic function), and you
- could handle all the realloc()'s for more memory and return a new
- buffer.
-
- Naturally, this all depends on what you're using the buffer for: if
- you're just going to print it out to a file or the screen, you'd do best
- to avoid the whole issue and fprintf() this stuff directly.
-
- -- Bill K.
-
- Bill Kaufman, | "...all conscious species are plastic and
- Corporate Lackey | all plastic species are conscious."
- wkaufman@us.oracle.com | -- Yew-Kwang Ng
-
-