Exception for 32 bit number C++

那年仲夏 提交于 2019-12-12 06:54:24

问题


Why do I get exception

Unhandled exception at 0x00000001 in TestingCOA.exe: 0xC0000005: Access violation (parameters: 0x00000008).

when I try to work with a 4294967295 or higher number. On my machine sizeof double is 8bytes which should be able to handle and work with2^64 -1 number but it is generating exception for a 32 bit number, why is that?

int main()
{
  double n,remainderA;
  int AfterDecimal1[64],RemExponent1;

  cout<< "Enter number\n";
  cin>> n;

  remainderA=a-(int)a;

   HandleFractionNumber(remainderA,AfterDecimal1,RemExponent1);
}


int HandleFractionNumber(double remainder,int (&Goku)[64],int &RemExponent)
{
int x=0;
for(int i=0;;i++)
{
    remainder*=2;
    if(remainder>1)
    {
        remainder-=1;
        Goku[x]=1;
        x++;
    }
    else
        if(remainder<1)
        {
            Goku[x]=0;
            x++;
        }
        if(remainder==1)
        {
            Goku[x]=1;
            break;
        }
        RemExponent=x;
}

回答1:


Double does not work like that. It is a structure with fields and certain format (known as IEEE double precision). Not all 64 bits are available for the mantissa.

Yet, it should have eaten the number you mentioned (4294967295) at input. Are you sure this is what you put? Is the program you quoted - all there is?




回答2:


remainderA = a - (int)a;

On a 32-bit machine the result here is undefined if a > 429496725. Try printing out remainderA, I'm betting it's large, leading HFN to overrun the buffer.




回答3:


You are running out of array boundaries.

x++ is done forever until it is greater than 64.

In code above, there is no reason for remainder to ever become one also. It is incorrect to compare a double with ==. It works reliably only for integer arithmetics.



来源:https://stackoverflow.com/questions/14003434/exception-for-32-bit-number-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!