I suppose I could just use a list however, at this point I am just curious why the following code does not work:
struct treeNode{
char symbol;
double freq;
int left;
int right;
};
treeNode *tree;
int nOS = 16;
tree = (treeNode *)malloc(sizeof(treeNode) * nOS);
list<treeNode> treeList;
After initializing all the elements in tree I try to push them to treeList and get a segmentation fault, it does not occur if tree is treeNode tree[nOS] but I am working with an unknown number of elements so I need to be able to use realloc, hence the use of malloc:
for (int i = 0; i < nOS; i++) {
treeList.push_back(tree[i]);
}
I tried casting tree[i] to a various number of things: (treeNode), (const treeNode) but I can’t figure out how to get it to work. Thank you!
I have done this in ideone, and it seems to work: (no segfaults)