I'm new to C/C++ and developing a C++ application. There I have a problem with new and malloc. My application is bit complex and there are some C structs as well. At some point, I wanted to allocate new memory for Class of type MyData (which contains a deque) and later I assigned that pointer to a pointer in a C struct. Smaller version of my code is as follows.
#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;
}
After allocating memory for both pointers I used GetDequeMyDataSet() method on malloc pointer (p_Data). My problem is whether it is ok to push_back items to the deque on this malloc pointer, as I have allocated memory for the pointer only? Can malloc handle dynamic memory allocation for deque?