Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #78188

Re: snprintf alternatives in iso9899

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: snprintf alternatives in iso9899
Date 2015-12-08 09:18 -0800
Organization None to speak of
Message-ID <lnegewoo3n.fsf@kst-u.example.com> (permalink)
References <n45p5d$hmp$1@speranza.aioe.org> <87lh956li9.fsf@bsb.me.uk> <allocating-printf-20151208160736@ram.dialup.fu-berlin.de> <87fuzc7uq3.fsf@bsb.me.uk> <salfmt-20151208175950@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>Did you ever show it to a compiler?  There's no way that code got
>
>   The last version of salfmt was not tested, indeed.
>   (I think, after I inserted »va_copy«, I did not 
>   test again.)
>
>   But previous versions were tested, and I hope to
>   find the time to one day maintain that page again. 
>
>   If you refer to a specific location in the code,
>   feel free to point out where you see the problem.

www.purl.org/stefan_ram/pub/c_faq_de

I haven't tried compiling it myself, but a couple of things jump out.

    s_type salfmt( s_type const f, ... )
    { va_list a; 
      char * b = 0;
      va_copy(a,f); { int const s = vsnprintf( 0, 0, f, a ); va_end(a); }
      if( s >= 0 )
      { size_t const k = 1 + s; if( b = malloc( k ))
        { va_start(a,f); { vsprintf( b, f, a ); va_end(a); }}} 
      return b; }

The code layout makes it difficult to read.

s_type is a typedef for char*.  (IMHO the code would be much easier to
read if it used char* directly.)

You call va_copy with an argument of type char*.  That might happen to
compile depending on how va_list is defined, but it's clearly wrong.

You define `s` inside a block and then refer to it outside the block.

(Personally I wouldn't publish code on a web page without at least
compiling it first.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

snprintf alternatives in iso9899 "Morten W. Petersen" <morphex@gmail.com> - 2015-12-08 06:18 +0100
  Re: snprintf alternatives in iso9899 Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-08 02:13 -0800
    Re: snprintf alternatives in iso9899 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-08 10:38 +0000
  Re: snprintf alternatives in iso9899 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-08 14:52 +0000
    Re: snprintf alternatives in iso9899 supercat@casperkitty.com - 2015-12-08 07:15 -0800
      Re: snprintf alternatives in iso9899 Tim Rentsch <txr@alumni.caltech.edu> - 2015-12-09 13:14 -0800
        Re: snprintf alternatives in iso9899 Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-09 16:28 -0500
          Re: snprintf alternatives in iso9899 Ian Collins <ian-news@hotmail.com> - 2015-12-10 10:35 +1300
            Re: snprintf alternatives in iso9899 Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-09 16:49 -0500
              Re: snprintf alternatives in iso9899 Ian Collins <ian-news@hotmail.com> - 2015-12-10 11:00 +1300
          Re: snprintf alternatives in iso9899 supercat@casperkitty.com - 2015-12-09 14:02 -0800
            Re: snprintf alternatives in iso9899 Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-10 02:41 -0800
          The FILE * pattern [was Re: snprintf alternatives in iso9899] Richard Heathfield <rjh@cpax.org.uk> - 2015-12-09 22:02 +0000
          Re: snprintf alternatives in iso9899 Keith Thompson <kst-u@mib.org> - 2015-12-09 14:49 -0800
    Re: snprintf alternatives in iso9899 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-08 16:47 +0000
      Re: snprintf alternatives in iso9899 Keith Thompson <kst-u@mib.org> - 2015-12-08 09:18 -0800
      Re: snprintf alternatives in iso9899 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-08 17:27 +0000
  Re: snprintf alternatives in iso9899 Keith Thompson <kst-u@mib.org> - 2015-12-08 08:41 -0800
    Re: snprintf alternatives in iso9899 "Morten W. Petersen" <morphex@gmail.com> - 2015-12-09 02:56 +0100
      Re: snprintf alternatives in iso9899 Ian Collins <ian-news@hotmail.com> - 2015-12-09 15:04 +1300
        Re: snprintf alternatives in iso9899 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-09 02:16 +0000
      Re: snprintf alternatives in iso9899 Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-09 04:49 -0800
        Re: snprintf alternatives in iso9899 "Morten W. Petersen" <morphex@gmail.com> - 2015-12-10 08:00 +0100

csiph-web