Re: How so ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Kelsey Bjarnason (kelseyb_at_lightspeed.bc.ca)
Date: 02/10/04


Date: Mon, 09 Feb 2004 19:13:28 -0800


[snips]

On Sun, 08 Feb 2004 11:14:27 +0000, Tim Robinson wrote:

> Anyone can learn C coding on Windows given a copy of Kernighan & Ritchie and
> a copy of Petzold.

I rather think not. For one thing, "coding C" turns out to be a *little*
more complex than simply cranking code that'll compile. For example, I've
seen way too many programs using undefined, improper or simply silly
constructs. Here's some examples:

char *s = malloc( 100 * sizeof(char) );

double *d = (double *) malloc( 100 * sizeof( *d ) );

float *f = malloc( 100 * sizeof(float) );

void main()

int main() { /* code */ return 1; }

char *func()
{
   char buff[128];
   /* store something in buff */
   return buff;
}

char *mystrdup( const char *s )
{
  char *d = NULL;
  if ( s )
  {
    d = malloc( strlen(s) );

    if (d)
      strcpy(d,s);
  }

  return d;
}

int x;
printf( "Please enter your number: " );
scanf( "%d", &x );

/* Determine number of bits in an int */
size_t bits = sizeof(int) / 8;

And on and on and on. All or at least most of these _look_ like they
should be good, but it requires a certain degree of experience to know why
each of them is either wrong, silly, or simply a bad idea. Simply reading
the books - even very attentively - is probably not going to suffice to
explain why they're bad, especially since the compiler probably won't say
thing one about most of 'em.



Relevant Pages

  • Re: Errors in code... help!
    ... Can someone tell me why the following errors come up at compile time: ... It is a type cast issue and I replaced the line with the following ... You have compiled this as a C++ program whereas Petzold wrote it as a C ... C++ enforces stricter type checking, so code that compiles OK as C ...
    (microsoft.public.vc.language)
  • Re: Newsgroup Guidelines and Etiquette
    ... "Dennis Landi" wrote: ... (Yet another improper use of FreeAndNil()) ... You can't even compile it ... ... Danijel ...
    (borland.public.delphi.thirdpartytools.general)