I am new to using smart pointers in C++ and my current issue is that I am converting C code to C++ (C++11/14/17) and I am having some problems understanding using shared_ptr with a pointer to pointer. I have derived a toy example which I believe illustrates the problem
Following is the header file
#include <memory>
using std::shared_ptr;
struct DataNode
{
shared_ptr<DataNode> next;
} ;
struct ProxyNode
{
shared_ptr<DataNode> pointers[5];
} ;
struct _test_
{
ProxyNode** flane_pointers;
};
And the actual code test.cpp
#include <stdint.h>
#include "test.h"
shared_ptr<DataNode> newNode(uint64_t key);
shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node);
int main(void)
{
// Need help converting this to a C++ style calling
ProxyNode** flane_pointers = (ProxyNode**)malloc(sizeof(ProxyNode*) * 100000);
// Here is my attempt (incomplete)
ProxyNode** flane_pointers = new shared_ptr<ProxyNode> ?
shared_ptr<DataNode> node = newNode(1000);
flane_pointers[1] = newProxyNode(node)
}
shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node)
{
shared_ptr<ProxyNode> proxy(new ProxyNode());
return proxy;
}
shared_ptr<DataNode> newNode(uint64_t key)
{
shared_ptr<DataNode> node(new DataNode());
return node;
}
I am getting these compiler errors -
test.cpp: In function ‘int main()’:
test.cpp:12:42: error: cannot convert ‘std::shared_ptr<ProxyNode>’ to ‘ProxyNode*’ in assignment
flane_pointers[1] = newProxyNode(node)
Compiled with
g++ -c -g test.h test.cpp
g++ version is 7.3.0 (on Ubuntu 18)
I need help converting the C style malloc allocation with a C++ style calling for a pointer to a pointer and then how to fix the compiler errors. My apologies if it looks like I am missing something obvious.