EXC_BAD_ACCESS(码= 1,地址= 0x1f400000010)

问题描述:

我有XCode的一个problema编程时在C++中,任何帮助将好评:)EXC_BAD_ACCESS(码= 1,地址= 0x1f400000010)

#include "TFloat.h" 
#include <math.h> 

TFloat::TFloat() 
{ 
_valor = 0.0; 
set_precision(52); 
recortar(); 
} 

TFloat::TFloat(size_t t) 
{ 
_valor = 0.0; 
set_precision(t); 
recortar(); 
} 

TFloat::TFloat(int i, size_t t) 
{ 
_valor = (double) i; 
set_precision(t); 
recortar(); 
} 

TFloat::TFloat(float f, size_t t) 
{ 
_valor = (double) f; 
set_precision(t); 
recortar(); 
} 

TFloat::TFloat(double f, size_t t) 
{ 
_valor = f; 
set_precision(t); 
recortar(); 
} 

double TFloat::dbl() const 
{ 
return _valor; 
} 

void TFloat::operator =(const TFloat& f) 
{ 
_valor = f._valor; 
_t = f._t; 
} 

void TFloat::operator =(const double& f) 
{ 
_valor = f; 
recortar(); 
} 

bool TFloat::operator ==(const TFloat& f) const 
{ 
return (_valor == f._valor); 
} 

TFloat TFloat::operator + (const TFloat& f) const 
{ 
double result = _valor + f._valor; 
    return TFloat(result, _t); 
} 

TFloat TFloat::operator - (const TFloat& f) const 
{ 
double result = _valor - f._valor; 
return TFloat(result, _t); 
} 

TFloat TFloat::operator * (const TFloat& f) const 
{ 
double result = _valor * f._valor; 
    return TFloat(result, _t); 
} 

TFloat TFloat::operator/(const TFloat& f) const 
{ 
double result = _valor/f._valor; 
return TFloat(result, _t); 
} 

TFloat TFloat::exponencial() const 
{ 
double result = exp(_valor); 
return TFloat(result, _t); 
} 

TFloat TFloat::operator+ (const double& f) const 
{ 
return this->operator+(TFloat(f,_t)); 
} 

TFloat TFloat::operator- (const double& f) const 
{ 
return this->operator-(TFloat(f,_t)); 
} 

TFloat TFloat::operator* (const double& f) const 
{ 
return this->operator*(TFloat(f,_t)); 
} 

TFloat TFloat::operator/(const double& f) const 
{ 
return this->operator/(TFloat(f,_t)); 
} 



void TFloat::recortar() 
{ 
bitset<64> * bits; 
bits = (bitset<64>*) &_valor; 

for(size_t i = 0 ; i < (52 - _t); i++) 
{ 
    (*bits)[i] = 0; 
} 
} 

而在此函数中使用 “recortar” 出现的问题:

TFloat::TFloat(double f, size_t t) 
{ 
    _valor = f; 
    set_precision(t); 
    recortar(); 
} 

当我做这个调用它的main.cpp:

size_t _precision = precision(51); 
TFloat Beta(0.0,_precision); 

我认为这是一个记忆问题,但我不知道如何解决它。谢谢!

+1

你不能盲目地把一个double转换成一个bitset。 –

+0

请制作一个[testcase](http://sscce.org) - 你期望如何调试_so many_ lines? –

+1

你已经忘记了'bitset'是一个复杂的类型!您不能简单地将double的基础位C-cast到“bitset”对象的基础位。它不仅仅是一系列的值字节。 –

起初你试图用这个代码实现什么?
如前所述

bits = (bitset<64>*) &_valor; 

都不行,你可以初始化位集要么用绳子方含0和1,或unsigned long类型保存位。使用浮点值进行位操作在任何情况下都没有多大意义。
Here很好的解释了不同类型的剧组是如何工作以及他们如何能够和不可以使用的。