Date: Wed, 15 Oct 1997 15:13:27 +0200 (MET DST) Message-Id: <199710151313.PAA18129@login.eunet.no> From: "Gisle Vanem" To: djgpp AT delorie DOT com Subject: Re: sprintf() string length? MIME-Version: 1.0 Content-type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Precedence: bulk Eli Zaretskii said: > On Tue, 14 Oct 1997, Peter Palotas wrote: > > > Is there a way to find out the length of the string that sprintf() will > > generate, without writing it anywhere? i.e. if I want to dynamically > > allocate the storage space for the string this would be very good (read > > essential) to know! > > You could make a crude estimation by walking through the format string > and assuming each % conversion specifier which doesn't have an > explicit character count produces the maximum possible length. A more > accurate way would be to know the default length for each conversion > letter (%s will need to get the length of the argument). djgpp misses the _bprintf(),_vbprintf() functions some other vendors have. These functions prevents buffer overflow and should solve Peters problem. E.g: char *dyn_sprintf (char *fmt, ...) { char buf [10000]; /* or whatever size is absolute max */ int len = _vbprintf (buf,sizeof(buf)-1,fmt,(&fmt)+1); /* ^ not sure if needed */ return strdup(buf); } I might see if I could make such functions for djgpp (modifying doprint). Gisle V.