Menu

DevCPP generated binary is getting hung.

2012-04-24
2012-09-26
  • Abhinandan H Patil

    Hi DevCPP team,

    You are doing great job.. Keep going..

    Here is the issue which you may want to have a look at.

    I am generating 100,000 random numbers using built in function and using the
    customised function.

    I expect the time to do so is linear function of input size.However the
    generated binary works for 10,000 numbers but not for 100,000 numbers.

    Here is the code which you may want to execute..

    include <stdio.h>

    include <stdlib.h>

    //#include <sys times.h="">

    define NO_OF_RANDOM_NOS 100000

    int randCustom(int seed); / Function prototype /
    / Variables for the program/
    int randomNumberArray,iter,iter1,dupValue=0,dupValueBuiltIn=0;
    int builtInRandomNumberArray,dupFlag,dupBuiltInFlag;
    long customStart,customEnd,builtInStart,builtInEnd;
    //struct tms
    structCustomStart,structCustomEnd,structBuiltInStart,structBuiltInEnd;
    int main()
    {
    / srand ( time(NULL) ); /
    //customStart = times (&structCustomStart);
    for(iter=0;iter<=NO_OF_RANDOM_NOS;iter++)
    {
    randomNumberArray=randCustom(NO_OF_RANDOM_NOSiter+1);
    /
    Uncomment the following value to see the behaviour for lower
    value of seeds
    randomNumberArray=randCustom(iter); */
    }
    //customEnd = times (&structCustomEnd);
    printf("\n Custom number generation time = %ld",customEnd -
    customStart);
    //builtInStart = times(&structBuiltInStart);

    for(iter=0;iter<=NO_OF_RANDOM_NOS;iter++)
    {
    builtInRandomNumberArray=rand();
    }
    //builtInEnd = times(&structBuiltInEnd);
    printf("\n built in number generation time = %ld",builtInEnd -
    builtInStart);
    for(iter=0;iter<NO_OF_RANDOM_NOS;iter++)
    {
    for(iter1=0;iter1<NO_OF_RANDOM_NOS;iter1++)
    {
    if(iter == iter1) continue;
    if(randomNumberArray == randomNumberArray)
    {
    dupFlag = 1;
    }
    if(builtInRandomNumberArray
    ==builtInRandomNumberArray)
    {
    dupBuiltInFlag=1;
    }
    }
    }
    dupValue=0;
    dupValueBuiltIn=0;
    for(iter=0;iter<NO_OF_RANDOM_NOS;iter++)
    {
    / printf("The dup indicating flag %d",dupFlag); /
    if(dupFlag==1) dupValue++;
    if(dupBuiltInFlag==1) dupValueBuiltIn++;
    }
    printf("The number of duplicates in the custom array %d\n",dupValue);
    printf("The number of duplicates in the built in array %d\n",dupValueBuiltIn);
    return 0;
    }
    int randCustom(int seed)
    {
    static long temp = 100001;
    if ( seed == 0 ) return 1;
    temp = (temp*125)%2796203;
    return ((temp%seed)+1);
    }

     
  • Aris

    Aris - 2012-04-24

    Lets think about it

    randomNumberArray = randCustom(NO_OF_RANDOM_NOS*iter+1);

    The seed of randCustom is the value of NO_OF_RANDOM_NOS*iter+1

    Values:
    NO_OF_RANDOM_NOS: 100.000
    iter: 0 to 100000
    and the 1 value. Lets calculate it if iter is 99.997

    NO_OF_RANDOM_NOS * iter +1 -> 100.000 * 99.997 + 1 -> 9.999.700.000 + 1 ->
    9.999.700.001

    But seed of randCustom function is int
    Is it possible an int variable to store 9.999.700.001???

    Usually in 32bit system the size of an int variable is 4bytes.

    4bytes or 4 * 8 bits -> 32bits.

    the max number that an int (32bits) can store is 2^32 if int is unsigned .

    2^32 = 4.294.967.295 < 9.999.700.001 So we have a problem here. int cannot
    store the value!!!

    But you use int and not unsigned int and thethe max number that an unsigned
    int
    can store is

    2^31 -1 = 2.147.483.647 < 9.999.700.001

     
  • Aris

    Aris - 2012-04-24

    sorry i made a mistake:
    (line: 13)

    the max number that an int (32bits) can store is 2^32 - 1 if int is unsigned

    2^32 = 4.294.967.295 < 9.999.700.001

     
  • Abhinandan H Patil

    Thanks Arism...

    The max value for

    The seed of randCustom is the value of NO_OF_RANDOM_NOSiter+1 is
    100000
    100000+1 ~ 10^10 which is far greater than 2^32.However the value
    should get truncated and depending upon the compiler "some" value should get
    assigned.

    Add this line in the beginning :
    int junk = 10000000000; and print it.Its get printed as "The value of junk is
    1410065408".

    I mean to say some value should get assigned.

    Here the issue is compiler generated code going into infinite loop.Can some
    one check that !!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.