Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > New output model

Reply
Thread Tools

New output model

 
 
nobody
Guest
Posts: n/a
 
      10-25-2003
"Arthur J. O'Dwyer" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>
> On Sat, 25 Oct 2003, nobody wrote:
> >
> > "Kevin Goodsell" <(E-Mail Removed)> wrote...
> > > nobody wrote:
> > > > Safer in what ways?
> > >
> > > If this isn't obvious to you, then I question whether you should be
> > > using printf() (and similar functions) at all.

> >
> > No, it's not obvious to me, how calling printf() via "wrapper" is safer

than
> > calling it directly with same argument values (e.g calling print_int(4)
> > instead of printf("%d", 4)).

>
> extern int printf(const char *, ...);
> extern int print_int(int);
>
> printf("%d", 4); /* legal and safe */
> print_int(4); /* legal and safe */
> printf("%d", "x"); /* legal and unsafe */
> print_int("x"); /* illegal */
>
> Variadic functions allow the unwary user to mess up big-time.
> The print_int() style automatically handles implicit conversion
> to the right type, and forces the compiler to warn about incorrect
> usage.
>
> > Also, if one is aware of limitations, implications and
> > pittfalls of something, does it imply that (s)he will use it in unsafe

way?
>
> Ever done any of the following?
>
> int *p;
> long l;
> int i;
> printf("%p\n", p);
> printf("%d\n", l);
> printf("%x\n", i);
>

Hard to say if ever, but I think not in last N years.

> Then your code is (technically speaking) incorrect. Variadic

UB

> functions don't perform as many useful conversions as you might

I know. They (or rather compiler) can't.

> think. The correct lines would be
>
> printf("%p\n", (void *) p);
> printf("%ld\n", l);
> printf("%x\n", (unsigned int) i);
>

Thanks for an answer. I've got the point. Though I still stick with
printf(.
(It was semantic issue /or my lack of understanding English/. This code
*is* safer, but that doesn't imply everyone not using it will write
less-safe
code.)


 
Reply With Quote
 
 
 
 
nobody
Guest
Posts: n/a
 
      10-25-2003
"Kevin Goodsell" <(E-Mail Removed)> wrote in message
news:qlCmb.2433$(E-Mail Removed) ink.net...
> nobody wrote:
> >
> > No, it's not obvious to me, how calling printf() via "wrapper" is safer

than
> > calling it directly with same argument values (e.g calling print_int(4)
> > instead
> > of printf("%d", 4)). Also, if one is aware of limitations, implications

and
> > pittfalls of something, does it imply that (s)he will use it in unsafe

way?
> > I didn't have yet encounter problems related to usage of printf() family
> > functions in programs I wrote so far. I will welcome any substantiation
> > of your questioning, or better yet, if you can show me why OP's code
> > is safer. I personally don't mind to learn from this NG (or any other
> > source,
> > for that matter).
> >
> >

>
> One meaningful metric for "safety" in this case is "the number of
> opportunities for one to make a mistake." printf() gives you many, many
> chances to make a mistake, and the compiler generally cannot help you
> locate such mistakes - they quietly invoke undefined behavior.
>
> How many ways can you screw up a call to print_xxx() (where xxx is some
> type)? Basically, you can call it with the wrong type. In this case, one
> of two things will happen. Either there is no implicit conversion and
> the compiler will issue a diagnostic, or there is an implicit conversion
> and the result will be well-defined. Nothing bad can happen other than
> the printed result appearing incorrect.
>
> How many ways can you screw up a printf() call? I doubt I can list them

all.
>
> * Wrong format specifier for the type
> * Wrong size specifier for the type
> * Transposed arguments
> * Too few arguments
> * Too many arguments (technically not an error, but probably a bug)
> * Incorrect argument type after promotion
>
> Most of these lead to undefined behavior, and the compiler cannot, in
> general, detect the error.
>
> You've never made one of these mistakes?
>
>
> ...are you sure?
>

Thanks for an answer. Will nor repeat what I've replied to Arthur, but yes,
I've made at least one of those mistakes in the past, but also I was talking
about "problems" (hidden bugs discovered by QA or customers) - none of
these (I'm talking about printf() only). All mistakes were found and
correected quickly when I tested my code, and hopefully I've better
knowledge now than some years back.


 
Reply With Quote
 
 
 
 
James Antill
Guest
Posts: n/a
 
      10-26-2003
On Sat, 25 Oct 2003 21:57:42 +0000, Kevin Goodsell wrote:

> One meaningful metric for "safety" in this case is "the number of
> opportunities for one to make a mistake." printf() gives you many, many


[snip]

> How many ways can you screw up a printf() call? I doubt I can list them all.
>
> * Wrong format specifier for the type
> * Wrong size specifier for the type
> * Transposed arguments
> * Too few arguments
> * Too many arguments (technically not an error, but probably a bug)
> * Incorrect argument type after promotion
>
> Most of these lead to undefined behavior, and the compiler cannot, in
> general, detect the error.
>
> You've never made one of these mistakes?
>
> ...are you sure?


I've used a static format checker that tells me if I've made any of the
above, for as long as I can remember. I'm sure I had problems before I did
that, and I'm sure I haven't since I got told about them at compile time.
If I had to go back to not having a static format checker, then I guess
I might look for a way to format data without printf() ... thankfully that
isn't a problem I have to solve.

--
James Antill -- http://www.velocityreviews.com/forums/(E-Mail Removed)
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

 
Reply With Quote
 
Doug Eleveld
Guest
Posts: n/a
 
      10-27-2003
Very interesting I think. If I was you I might try returning a struct
instead of a pointer so you could actually add data to the struct as
you go along. i.e. the start function could have a FILE* or char* of
where to redirect.

It would also then use the struct reference '.' which I think looks
better than '->', although it may be confusing in this context. I
dont know if it would reduces efficieny or not.

You do realize that you could use this for formatted input too?

I think this is very interesting, could you keep me informed as to any
improvements you make?

Doug Eleveld
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New photos of my new masterpiece model! Alexander Blokhin Digital Photography 1 06-19-2008 12:24 PM
Convert Java Model to Java Model without XML erinbot@gmail.com Java 1 10-06-2006 09:00 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments