Allocate Memory for Vector of a Structure

Hi everyone,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct activity{
		int id; //Activity number
		int duration; //Duration of activity
		vector<int> resource_requirements; //Resource requirements
		int number_of_successors; //Number of successors
		int successors[200]; //Successors of activities
	};
vector<activity> activity(30);

//NOW I WANT TO ALLOCATE MEMORY FOR STRUCTURE ELEMENTS
for(int i=0; i<30; i++){activity[i].id = new int;}
for(int i=0; i<30; i++){activity[i].duration = new int;}
for(int i=0; i<30; i++)
{
	for(int j=0; j<4; j++){activity[i].resource_requirements[j] = new int;}
}

for(int i=0; i<30; i++){activity[i].number_of_successors = new int;}

for(int i=0; i<30; i++)
{
	for(int j=0; j<200; j++){activity[i].successors[j] = new int;}
}


and in that line there is an error

for(int j=0; j<4; j++){activity[i].resource_requirements[j] = new int;}

thanks in advance ;)
Last edited on
closed account (zb0S216C)
airerdem wrote:
vector<activity> activity(30); (sic)
activity needs to be a pointer. Only pointers can allocate memory dynamically. Also, why would you allocate each element dynamically? The STL vector does that for you.

Wazzak
Last edited on
Actually i am pretty new in coding could you explain how to point activity vector and the other allocating thing about STL ?
STL vectors take care of their own memory, thus removing the need for Static vs Dynamic declaration. You can remove all "new" stuff; you'll never need it in combination with vectors. If you replace all the "new int" stuff with a value, it should work.

To "reserve spots", use .resize(). Or, if possible, use the constructor to automatically assign spaces:

1
2
vector<int> myInts(30, 0); // Initializes a vector with 30 ints set to 0.
vector<activity> myActivities(30); // Initializes a vector of 30 "empty" activities. 


One exception is your resource_requirements variable. A "basic" initalization of a vector leaves it with size 0. This can easily be taken care of:

activity[i].resource_requirements.resize(30); // Will assign 30 (uninitialized!) int spots.

If you find that not every activity has an equal (or pre-determined) amount of resource_requirements, you can always use the .push_back(T) command to add an additional T spot (e.g. activity[i].resource_requirement[j].push_back(5) will add a 31st integer "5").
Last edited on
So I don't need to use new operator with vectors. Actually I used them to speed up the program when I try to reach every element of vectors. How about the using pointers to speed up during reaching ? Does it matter in terms of speed ?
I'm sorry, I don't really know what you mean. I don't see how initializing pointers would ever speed up anything. Behind the scene, vectors are purely pointers anyway.

As far as I know, pointers only make a difference in performance when passing arguments to functions, where pointers/references eliminate the creation of copies behind the scenes.
Ok thank you very much I got it ;)
Topic archived. No new replies allowed.