www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/05/22:39:10

Date: Sun, 5 May 1996 22:24:26 -0400 (EDT)
From: Justin Ward <justin AT yoss DOT canweb DOT net>
To: Nicholas Marrone <nmarrone AT smartlink DOT net>
cc: djgpp AT delorie DOT com
Subject: Re: Use of random
In-Reply-To: <4mikhp$pa5@frodo.smartlink.net>
Message-ID: <Pine.BSF.3.91.960505221646.12398A-100000@yoss.canweb.net>
MIME-Version: 1.0

You didn't give any of the output. But I'm willing to guess you flipped
out when you saw it giving you numbers above 32767. That's because DJGPP
is 32 bit, and hence uses 32bit (four byte) ints. So, the maximum value
for an int is not 2^15 - 1, but 2^31 - 1, which is somewhere over four
trillion. Also your malloc statements are fucked. I think what you want
is 2000 ints. In that case, you should malloc(NUM * sizeof(int)). I'm 
surprised that you didn't seg fault or anything when you didn't try to
put in a value in origp[1001], which would be 4004 bytes past origp[0],
which you didn't allocate.. wierd.

Justin


On Sun, 5 May 1996, Nicholas Marrone wrote:

> 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!!!");
>       }
> 
>    clock();             /* Initialize the clock */
>    (int *)ticks = clock();
>    srand(ticks);
> 
>    for (ctr = 0; ctr < NUM; ctr++) {
>       origp[ctr] = rand();
>    }
> 
>    radix_bit_sort(origp, onesp, zerop, NUM);
> 
> 
>    for (ctr = 0; ctr < 20; ctr++)
>       printf("%d\n", origp[ctr]);
> 
>    free(origp);
>    free(onesp);
>    free(zerop);
> 
>    return 0;
> }
> 
> I really need some serious help. Thanks.
> 
> 	nicholas
> 
> 

- Raw text -


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