Topic: Why no estimator for sprintf()?


Author: Paolo Carlini <pcarlini@unitus.it>
Date: Wed, 27 Mar 2002 13:31:27 GMT
Raw View
Nate Hayes wrote:

>I have been using the online documentation for Visual C++ 6.0... which is
>based on a draft standard that is many, many years old. I see that in the
>new 9899 standard they have made this part of the spec.
>
I see.
A word of caution is in order: you can find around /many/ "snprintf"
/not/ compliant in their detailed behaviour with what prescribed by the
new 9899 standard (and implemented in glibc2.1 and 2.2), e.g. returning
-1 in case the required lenght exceeds the size specified in the call.
If you write your code assuming the C99 behaviour and recompile on such
platforms you almost certainly will obtain /crazy/ things at run time...

Ciao, Paolo.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Nate Hayes" <nhayes@sunfishstudio.com>
Date: Mon, 25 Mar 2002 21:03:32 GMT
Raw View
I am curious to know why in C or C++ there is no function defined by the
standard that returns the storage required to safely hold the output of a
call to sprintf(). It seems that since implementing the sprintf() routine is
the burden of the host platform, it is in the best position to know how much
memory would be required to hold the output of sprintf().

I am also curious to know why there at least isn't an snprintf() (or it's
equivalent) defined so that a programmer could at least guarantee an upper
limit to the memory required:

    const int maxBuf = 100;
    char buf[ maxBuf ];
    snprintf( buf , maxBuf , "This is the %s text" , "format" );

--nate




======================================= MODERATOR'S COMMENT:
 Watch crossposts on followups!

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Paolo Carlini <pcarlini@unitus.it>
Date: Mon, 25 Mar 2002 22:11:49 GMT
Raw View
Nate Hayes wrote:

>I am also curious to know why there at least isn't an snprintf() (or it's
>equivalent) defined so that a programmer could at least guarantee an upper
>limit to the memory required:
>
There is! The new "C99" standard (ISO/IEC 9899) includes a snprintf of
exactly such characteristics. You can find it implemented in glibc2.1.x
and glibc2.2.x .

Ciao, Paolo.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: those who know me have no need of my name <not-a-real-address@usa.net>
Date: Tue, 26 Mar 2002 09:48:01 GMT
Raw View
<u9t148m09arp1e@corp.supernews.com> divulged:

>I am curious to know why in C or C++ there is no function defined by the
>standard that returns the storage required to safely hold the output of a
>call to sprintf().

the standard doesn't call for it, though it certainly seems like a good
addition, perhaps if the buffer is NULL.

>I am also curious to know why there at least isn't an snprintf()

c99 has snprintf, which does calculate the space needed when supplied with
a maximum length parameter of zero ...

>    const int maxBuf = 100;
>    char buf[ maxBuf ];
>    snprintf( buf , maxBuf , "This is the %s text" , "format" );

  size_t sz = snprintf(0,0,format,data...);
  assert(0 <= sz);
  char *buf = malloc(sz+1);
  assert(NULL != buf);
  (void)snprintf(buf,sz+1,format,data...);
  free(buf);

--
bringing you boring signatures for 17 years

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 26 Mar 2002 05:41:31 CST
Raw View
Nate Hayes wrote:
>
> I am curious to know why in C or C++ there is no function defined by the
> standard that returns the storage required to safely hold the output of a
> call to sprintf(). It seems that since implementing the sprintf() routine is
> the burden of the host platform, it is in the best position to know how much
> memory would be required to hold the output of sprintf().
>
> I am also curious to know why there at least isn't an snprintf() (or it's
> equivalent) defined so that a programmer could at least guarantee an upper
> limit to the memory required:

For C, the answer to both your questions is that there is such a
function, and it's name is, oddly enough, snprintf() (error handling
omitted for the sake of clarity):

 #include <stdio.h>
 #include <stdlib.h>
 // Use as estimator:
 int n = snprintf(NULL, 0, format_string, arguments);
 char *buffer = malloc(n);
 // Use as guaranteed limit:
 snprintf(buffer, n, format_string, arguments);

 // use buffer

 free(buffer);

You've also cross-posted this question to comp.std.c++, but there just
isn't as much need for such a function there. You'd use an entirely
different and somewhat simpler approach:

 #include <sstream>
 std::ostringstream buffer;
 buffer << arguments;

However, if you still want it, buffer.pcount() will tell you the current
count of characters in the buffer.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Nate Hayes" <nhayes@sunfishstudio.com>
Date: Tue, 26 Mar 2002 15:54:45 GMT
Raw View
Ah, yes! You're right.

I have been using the online documentation for Visual C++ 6.0... which is
based on a draft standard that is many, many years old. I see that in the
new 9899 standard they have made this part of the spec.

Nate

"Paolo Carlini" <pcarlini@unitus.it> wrote in message
news:3C9F93BC.9070705@unitus.it...
Nate Hayes wrote:

>I am also curious to know why there at least isn't an snprintf() (or it's
>equivalent) defined so that a programmer could at least guarantee an upper
>limit to the memory required:
>
There is! The new "C99" standard (ISO/IEC 9899) includes a snprintf of
exactly such characteristics. You can find it implemented in glibc2.1.x
and glibc2.2.x .

Ciao, Paolo.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Ruslan Abdikeev" <ruslan_abdikeevREMOVE_IT@hotmail.com>
Date: Tue, 26 Mar 2002 18:15:40 GMT
Raw View
"Nate Hayes" <nhayes@sunfishstudio.com> wrote in message news:u9t148m09arp1e@corp.supernews.com...
>
> I am also curious to know why there at least isn't an snprintf() (or it's
> equivalent) defined so that a programmer could at least guarantee an upper
> limit to the memory required:
>

IMHO, the reason for that might be in an existence of
much better ways to do formatted output in C++.

However, poor little boy C99 (ISO 9899 7.19.6.5) includes
a snprintf() function, which does exactly what you want.

Sincerely,

Ruslan Abdikeev
Brainbench MVP for Visual C++
http://www.brainbench.com




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Stephen Howe" <SPAMstephen.howeGUARD@tnsofres.com>
Date: Tue, 26 Mar 2002 21:01:58 GMT
Raw View
> However, if you still want it, buffer.pcount() will tell you the current
> count of characters in the buffer.

Pardon me, but what is pcount() ? I find it as a member of  strstream and
ostrstream but not ostringstream. This to me is lop-sided. If basic_istream
has gcount() then basic_ostream should have pcount().

Stephen Howe





---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: seebs@plethora.net (Peter Seebach)
Date: Tue, 26 Mar 2002 19:59:18 CST
Raw View
In article <u9t148m09arp1e@corp.supernews.com>,
Nate Hayes <nhayes@sunfishstudio.com> wrote:
>I am curious to know why in C or C++ there is no function defined by the
>standard that returns the storage required to safely hold the output of a
>call to sprintf(). It seems that since implementing the sprintf() routine is
>the burden of the host platform, it is in the best position to know how much
>memory would be required to hold the output of sprintf().

Because, if we were to invent one, and call it snprintf, and put it in the
standard, no one would use it.

-s
--
   Copyright 2002, all wrongs reversed.  Peter Seebach / seebs@plethora.net
   $ chmod a+x /bin/laden      Please do not feed or harbor the terrorists.
     C/Unix wizard, Pro-commerce radical, Spam fighter.  Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 26 Mar 2002 20:34:48 CST
Raw View
Stephen Howe wrote:
>
> > However, if you still want it, buffer.pcount() will tell you the current
> > count of characters in the buffer.
>
> Pardon me, but what is pcount() ? I find it as a member of  strstream and
> ostrstream but not ostringstream. This to me is lop-sided. If basic_istream
> has gcount() then basic_ostream should have pcount().


You're right; I remembered there being such a function, but I must have
been thinking of ostrstream (which is deprecated). I should remember to
look these things up before I post a message. I did look up the parts I
wrote about snprintf(), at least. :-}

The simplest alternative I could find was
buffer.rdbuf().pptr()-buffer.rdbuf().pbase(). Actually,
buffer.str().size() is simpler, but seems wasteful, unless a
highly-optimized compiler could somehow simplify it to the same code.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]