我正在开发一个在linux环境下运行的C++应用程序.我需要存储6个deciaml位置的大值.我用了一个双.但是在赋值之后,double变量不包含确切的值.它完善了.
例如:
double dValue = 79447461534242.913072; //Assignement of value
Run Code Online (Sandbox Code Playgroud)
但在那之后,当我看到dValue中的值时,它就像79447461534242.906
有人可以告诉我为什么会这样,并建议我正确的数据类型,它可以保持精确的值而不会失去精度.
我是C/C++的新手,也是开发C++应用程序的人.我遇到new和malloc的问题.我的应用程序有点复杂,也有一些C结构.在某些时候,我想为MyData类型的Class(包含一个deque)分配新的内存,后来我将该指针分配给C结构中的指针.我的代码的较小版本如下.
#include <deque>
class MyData
{
public:
MyData(){};
~MyData() {};
std::deque<int>& GetDequeMyDataSet() {return deque_MyDataSet; };
private:
std::deque<int> deque_MyDataSet;//contains ohlc data for the symbol
};
int _tmain(int argc, _TCHAR* argv[])
{
MyData* pMyData = new MyData();
MyData* p_Data = (MyData*)malloc(sizeof(MyData*));
p_Data = pMyData;
p_Data->GetDequeMyDataSet().push_back(10);
p_Data->GetDequeMyDataSet().push_back(11);
//.... Several other push back and operations releated to this deque goes here.
delete pMyData;// At the end I free both memories.
free(p_Data);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在为两个指针分配内存后,我在malloc指针(p_Data)上使用了GetDequeMyDataSet()方法.我的问题是,将mall_back项目推送到此malloc指针的deque是否可以,因为我只为指针分配了内存?malloc可以为deque处理动态内存分配吗?