linear topology

Hi,

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];


any help please?

Sorry for my bad english.
This sounds like a job for dynamic memory allocation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace 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;
}
Thanks for the reply @shacktar,

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 ?

Edit:

i tried to create a temp dynamic array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ltop* nodes = new ltop[howMany];

for(int i=0;i<howMany-1;i++)
    nodes[i].id = i;

ltop* temp = new ltop[howMany-1];

for(int i=0;i<howMany-1;i++)
    temp[i] = nodes[i];

nodes= new pipeline[howMany-1];

for(int i=0;i<x-1;i++)
         nodes[i] = temp[i];

delete [] temp;

for(int i=0;i<atoi(argv[1]);i++)
         cout << nodes[i].id<< " ";


but the output i receive is if we use howMany=3 is :

0 1 2

0 1 0


this happens because the last element is not deleted..

Any Help please, how to rezise the array?
Last edited on
Hi,

except from the previous question i have a new one.

i tried :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace 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 ?
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
using namespace 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.
Topic archived. No new replies allowed.