Reputation: 767
I want to have a linked list, with a variable which has dynamic size,
because I want to just allocate different sizes for a variable in different nodes.
for example node1 has a array variable with size 1, but node 2 has a array variable with size 10, and node3 never allocates this array.
like this:
struct st{
int * var_dynamic;
int x;
};
now I want to initialize them. for the static one, it is like this:
struct st st1;
st1.x=1;
but how can I initialize the dynamic one?
Is it something like this?
st1.var_dynamic= new int [100];
and if yes, Is this way correct and efficient?
Upvotes: 1
Views: 368
Reputation: 171167
The most idiomatic, straightforward, and safe solution is to simply use std::vector
:
struct st
{
std::vector<int> var_dynamic;
int x;
};
For using std::vector
, consult a reference documentation, or your favourite book.
Upvotes: 2