linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Charlie Gordon" <gmane@chqrlie.org>
To: linux-c-programming@vger.kernel.org
Subject: Re: Some question about one method.
Date: Tue, 25 May 2004 01:24:49 +0200	[thread overview]
Message-ID: <c8u049$b00$1@sea.gmane.org> (raw)
In-Reply-To: 20040524173017.GW1912@lug-owl.de

Hi JBG,

> There are sane uses of sprintf()...

There is almost no overhead to using snprintf.  And it is so difficult to
prove that a given sprintf format cannot overflow...  Just try this one:
what if ints are 64 bits now, is the buffer large enough to accomodate all
these digits ?
Yet there is much worse function : strncpy.  This sucker does NOT do what
you think it does. Pull the man page, read it, read it again... unbelievable
isn't it ? Test it, grep any source files for this buddy, I bet you'll find
subtle bugs stuck to it like fly paper.

> C is (and always was) by experts for experts.
> C's syntax is quite simple, but only people who really
> understand C's pointer and allocation concepts should use it.

I agree completely on this, programming in C is like riding down
the guardrail, except it's more like a razor blade.
It's just so hard to always keep a sharp eye for all the pitfalls one
comes across in even the simplest one liners.

I urge you to get a look at http://www.joelonsoftware.com/
I especially like his "Guerrilla Guide to Interviewing"
(http://www.joelonsoftware.com/articles/fog0000000073.html) :
"I've discovered that understanding pointers in C is not a skill, it's an
aptitude. For some reason most people seem to be born without the part of
the brain that understands pointers. This is an aptitude thing, not a skill
thing - it requires a complex form of doubly-indirected thinking that some
people just can't do."

>> - what is wrong with : enum BOOL { FALSE=0, TRUE=1 };
> (I don't think there's something technically wrong with the enum
> BOOL, but you shouldn't use it except with other enum BOOL
> xxx variables ...)

Well you are going to love this one!
defining BOOLeans TRUE and FALSE as an enum makes them available for
symbolic debugging, but you should still #define them to avoid nasty
surprises with the preprocessor:

#if TRUE
// if you don't, this code will never compile
#endif

> char *a;
> short *b;
> int *c;
> long *d;
> long long *e;
>
> --> What's the difference between a++ and e++?
it makes them point to the next element in the arrays of respective types,
and as such increments the binary value by a different amount.

More interestingly what is the difference between e[5] and 5[e] ?

> struct {
>  char first;
>  char second;
> } s1;
> struct {
>  char both[2];
> } s2;
> what's the difference ?
On most architectures, not much.  But C makes no guaranties as to the actual
layout of structures.
In particular (&s1.second - &s1.first) may be different than 1.
Similarly sizeof(s1) can be different from sizeof(s2).

Glad you found my quiz useful.

Chqrlie.

PS:) did you know about this new C library extension : imprecations
you can pass extra information to some of the C runtime APIs, thus
preventing some of the most annoying problems in C.
for example, memory allocation issues are finally solved :

p = malloc(n), "nofail";
free(p), "pack";
p = realloc(p, newsize), "nomove";

best of all, your compiler most likely already supports imprecations ;-)






  reply	other threads:[~2004-05-24 23:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-21 10:24 Some question about one method Alphex Kaanoken
2004-05-21 21:17 ` Jan-Benedict Glaw
2004-05-24  8:06   ` Charlie Gordon
2004-05-24 17:30     ` Jan-Benedict Glaw
2004-05-24 23:24       ` Charlie Gordon [this message]
2004-05-25  0:06         ` Jeff Woods
2004-05-25  0:41         ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2004-05-21 10:54 Ranga Reddy M - CTD ,Chennai.
2004-05-21 21:19 ` Jan-Benedict Glaw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='c8u049$b00$1@sea.gmane.org' \
    --to=gmane@chqrlie.org \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).