i want to create a linear topology. Each node must have an id and memory. I need help with the struct, which i want to use to create a node. So, what i have so far:
1 2 3 4
struct ltop{
int id; //node id
int memory[50];
}node;
The problem is that the user will define the number of nodes for the topology, so for example if the user defines the number of nodes to be 10, then i will need 10 "structs" like the above. How can i do this ?
i tried:
1 2 3 4
struct ltop{
int id; //node id
int memory[50];
}node[10];
#include <iostream>
usingnamespace std;
struct ltop{
int id; //node id
int memory[50];
};
int main()
{
int howMany = 0;
cout<<"How many nodes do you need?"<<endl;
cin>>howMany;
//assuming no error in input
ltop* nodes = new ltop[howMany];
//now you can access the nodes like an array
//e.g.
//nodes[0].id = 1;
//nodes[0].memory[0] = 0; ...
//when you're done
delete [] nodes;
}
i would like to ask if we want to delete a specific argument for example if the user type
howMany=3 , this means that it will create nodes[0], nodes[1], nodes[2]. If want to delete one element. For example to have only node[0] and node[1] how can we delete the nodes[2] ?
In c i think we can use realloc in c++ how can this be done ?
#include <iostream>
usingnamespace std;
struct ltop{
int id; //node id
int memory[50];
};
int main()
{
int howMany = 0;
cout<<"How many nodes do you need?"<<endl;
cin>>howMany;
//assuming no error in input
ltop* nodes = new ltop[howMany];
//give id
for(int i=0;i<howMany;i++)
nodes[i].id = i;
//print the id
for(int i=0;i<howMany;i++)
cout << nodes[i].id << " ";
cout << "\n\n";
//when you're done
delete [] nodes;
//print the id ?????
for(int i=0;i<howMany;i++)
cout << nodes[i].id << " ";
return 0;
}
the output i receive is :
How many nodes do you need?
10
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Althought in line 29 we delete the array it still keep its values.. What going wrong ?
In c i think we can use realloc in c++ how can this be done ?
In C++ you can use realloc as well (if the memory was created with malloc), but you have to remember that malloc won't call any objects' constructors. For your case, your struct has nothing but ints so using malloc/realloc is fine. However, as soon as your struct contains objects, that won't work any more.
Since you want to resize the array, you might as well use a std::vector.
#include <iostream>
#include <vector>
usingnamespace std;
struct ltop{
int id; //node id
int memory[50];
};
int main()
{
int howMany = 0;
cout<<"How many nodes do you need?"<<endl;
cin>>howMany;
//assuming no error in input
vector<ltop> nodes;
nodes.resize(howMany);
//now you can access the nodes like an array
//e.g.
//nodes[0].id = 1;
//nodes[0].memory[0] = 0; ...
//delete node at index howMany-1
nodes.pop_back();
//delete node at index 5
nodes.erase(nodes.begin()+5);
return 0;
}
Althought in line 29 we delete the array it still keep its values.. What going wrong ?
Reading from deallocated memory produces undefined behavior. It should probably cause a segmentation fault in most cases. In your case, you got lucky and it didn't crash.