www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/06/00:27:01

Xref: news2.mv.net comp.lang.c:66629 comp.os.msdos.djgpp:3474
From: watzka AT stat DOT uni-muenchen DOT de (Kurt Watzka)
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: Use of random
Date: 6 May 1996 00:34:37 GMT
Organization: Institut fuer Statistik der LMU Muenchen (Germany)
Lines: 74
Distribution: world
Message-ID: <4mjhet$g4u@sparcserver.lrz-muenchen.de>
References: <4mikhp$pa5 AT frodo DOT smartlink DOT net>
Reply-To: watzka AT stat DOT uni-muenchen DOT de
NNTP-Posting-Host: sun2.lrz-muenchen.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

nmarrone AT smartlink DOT net (Nicholas Marrone) writes:

>Hullo,
>	To test out my radix_bit_sort function, I've been trying to generate a
>list of random numbers to sort, and then sort them. The list is rather
>long, 4000, and I'd like it to be ints, but I can' seem to get it! The
>program compiles and runs without error, but it gives me #s far larger
>than integers when I print out some results. I'm using DJGPP v2.
>Here's some of the code...

>#include <stdio.h>
>#include <stdlib.h>
>#include <time.h>

>#define NUM 2000

>int main()
>{

>   int ctr;
>   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.

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:

     origp = malloc(NUM * sizeof *origp);

>   clock();             /* Initialize the clock */
>   (int *)ticks = clock();
>   srand(ticks);

>   for (ctr = 0; ctr < NUM; ctr++) {
>      origp[ctr] = rand();
>   }

This will fail if sizeof(int) happens to be greater than 2.

>   radix_bit_sort(origp, onesp, zerop, NUM);

I will assume that this function does not modify origp[0] to origp[20].

>   for (ctr = 0; ctr < 20; ctr++)
>      printf("%d\n", origp[ctr]);

Since "origp[ctr]" is an int, and since "%d" is a valid format string for
printing an int, you might want to check your assumptions about the
possible range of ints in C.

The definition of the language gives us some minimum representable ranges
for each basic type. Implementors are free to provide higher precision or
wider ranges.

Kurt

-- 
| Kurt Watzka                             Phone : +49-89-2180-6254
| watzka AT stat DOT uni-muenchen DOT de

- Raw text -


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