How to properly allocate memory in a loop using vector? [closed]

  abstract-syntax-tree, c++, vector

Im working with the creation of the abstract syntax tree for a specific domain language. So the snippet here just allocating a memory for the children of the node. However, I can’t grasp what should I do in here, everytime the push_back happens, the previous content will be modified and be the same with the newly created object.

int main() {
vector <AST*> node;
for (int i = 0; i < 5; i++)
{
    char a[255] = "Token";
    sprintf(a, "%s%d", a, i);
    Token b = Token(1, a);
    node.push_back(new AST(b));
}
return 0; }

enter image description here

Source: Windows Questions C++

LEAVE A COMMENT