www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/06/12:31:09

From: kagel AT quasar DOT bloomberg DOT com
Date: Mon, 6 May 1996 08:35:38 -0400
Message-Id: <9605061235.AA01555@quasar.bloomberg.com >
To: watzka AT stat DOT uni-muenchen DOT de, nmarrone AT smartlink DOT net
Cc: djgpp AT delorie DOT com
In-Reply-To: <4mjhet$g4u@sparcserver.lrz-muenchen.de> (watzka@stat.uni-muenchen.de)
Subject: Re: Use of random
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   From: watzka AT stat DOT uni-muenchen DOT de (Kurt Watzka)
   Date: 6 May 1996 00:34:37 GMT

   nmarrone AT smartlink DOT net (Nicholas Marrone) writes:

   >   int *origp;
   >   int *onesp, *zerop;
   >   int ticks;       /* for use with the clock in randomizing shtuff */

   >   if ( !(origp = (int *)malloc(NUM * 2)) ||
   >	!(onesp = (int *)malloc(NUM * 2)) ||
   >	!(zerop = (int *)malloc(NUM * 2)) )
   >      {
   >	 printf("Not enough memory to run this program!!!");
   >      }

   The casts of the return value from malloc to "int *" is not needed in
   a C program, nor does it contribute anything good or bad to your 
   program.

Actually, I would encourage Mr. Marrone to continue to cast the return of
malloc().  I believe that it contributes SOMETHING VERY GOOD, it improves the
documentation value of the code with little added effort.  It documented his
intent well enough that you spotted his biggest problem!!!!!!!!!!!!  I've been
programming in "C" for 15 years and I hope I never get jaded enough or lazy
enough to stop adding types casts wherever an automatic cast would occur.  Then
I, an anyone else reading my code, will know what I have done and that it was
intentional (I might still be WRONG, but intentional and how much easier to fix
when the error is made glaring this way)!

   OTOH, I hope you use only "(NUM * 2) / sizeof(int)" elements of each 
   array.

   I assume that you assume that sizeof(int) is 2 in your implementation.
   A good way to aviod this problem is to use the sizeof operator in
   malloc() and similar statements:

And of course in DJGPP, as a 32 bit compiler, int's are 4 bytes.

<STUFF DELETED>

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019