problem with push_back

Without going into too much detail about my program, does anybody have an idea of what is causing this.

I have loop which is loading values into a vector using push_back. Here is some pseudo code:

1
2
3
4
5
for(int k = 0; k < num; k++)
{		
stack.m_stack.push_back( particle.at(k) );
}
				


'stack' is vector of particle parameters defined in a class. 'particle' is another vector of a set of parameters defined in another class. Whenever I run my program an exception is thrown when the push_back function is reached. However it is not thrown on the first iteration; it usually takes a few dozen iterations for something to go wrong.

I have analyzed this loop in the debugger to make sure that particle.at(k) does indeed contain a value and it does every time an exception is thrown. Whenever I comment out the push_back function, my program runs fine. I have tried a lot of different things for days now and I still cannot figure out why an exception is being thrown. Any ideas????
Last edited on
I'll go ahead and just post my entire function so that you can get a better idea of what the problem may be. The exception is being thrown on line 92 in the code shown below. Any suggestions regarding optimization would also be appreciated. Thanks.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
vector<vector<vector<float>>> pop_stack(float delta_energy, int size, int resolution, vector<vector<vector<float>>> &ref_dose_grid, Stack stack, SearchStructureContainer &search_structure_container, DataBaseContainer &data_base_container)
{
	cout << "   Poping Stack" << endl;

	int tertiary_count = 0;//<--------------------------------------------------------------------DEBUG

	while(!stack.m_stack.empty())
	{
		Secondary &ref_secondary = stack.m_stack.back();

		stack.m_stack.pop_back();

		float secondary_energy = ref_secondary.m_energy;
		int secondary_type = ref_secondary.m_int_particle_recognition;
		int secondary_bin_index = secondary_energy / delta_energy;
		float secondary_x_direction = ref_secondary.m_direction_x;
		float secondary_y_direction = ref_secondary.m_direction_y;
		float secondary_z_direction = ref_secondary.m_direction_z;
		float energy_deposition = 0;
		int step_id = 0;
		int track_id = 0;
		int dose_grid_index_x = 0, dose_grid_index_y = 0, dose_grid_index_z = 0;
		float x_vec_rotated = 0, y_vec_rotated = 0, z_vec_rotated = 0;
		int random_element_index = 0; 
		DataBase &ref_data_base = data_base_container.m_data_base_container.at(secondary_type);

		int bin_size = search_structure_container.m_search_structure_container.at(secondary_type).m_search_structure.at(secondary_bin_index).number_of_elements;
		while(bin_size == 0)
		{
			secondary_bin_index++;
			bin_size = search_structure_container.m_search_structure_container.at(secondary_type).m_search_structure.at(secondary_bin_index).number_of_elements;
		}

		if(bin_size == 1)
			random_element_index = 0; 

		else
			random_element_index = rand() % (bin_size-1); 

		step_id = search_structure_container.m_search_structure_container.at(secondary_type).m_search_structure.at(secondary_bin_index).m_search_bin.at(random_element_index).m_step_id;
		track_id = search_structure_container.m_search_structure_container.at(secondary_type).m_search_structure.at(secondary_bin_index).m_search_bin.at(random_element_index).m_track_id;

		float x_direction_initial = ref_data_base.m_data_base.at(track_id).m_track.at(step_id).m_xvec;
		float y_direction_initial = ref_data_base.m_data_base.at(track_id).m_track.at(step_id).m_yvec;
		float z_direction_initial = ref_data_base.m_data_base.at(track_id).m_track.at(step_id).m_zvec;

		float x_direction_new = secondary_x_direction;
		float y_direction_new = secondary_y_direction;
		float z_direction_new = secondary_z_direction;

		float r = pow( (x_direction_initial - x_direction_new), 2) + pow( (y_direction_initial - y_direction_new), 2) + pow( (z_direction_initial - z_direction_new), 2);

		float Q_0_0 = 1 - (1/r) * 2 * (x_direction_initial - x_direction_new) * (x_direction_initial - x_direction_new);
		float Q_0_1 = - (1/r) * 2 * (x_direction_initial - x_direction_new) * (y_direction_initial - y_direction_new);
		float Q_0_2 = - (1/r) * 2 * (x_direction_initial - x_direction_new) * (z_direction_initial - z_direction_new);

		float Q_1_0 = - (1/r) * 2 * (y_direction_initial - y_direction_new) * (x_direction_initial - x_direction_new);
		float Q_1_1 = 1 - (1/r) * 2 * (y_direction_initial - y_direction_new) * (y_direction_initial - y_direction_new);
		float Q_1_2 = - (1/r) * 2 * (y_direction_initial - y_direction_new) * (z_direction_initial - z_direction_new);

		float Q_2_0 = - (1/r) * 2 * (z_direction_initial - z_direction_new) * (x_direction_initial - x_direction_new);
		float Q_2_1 = - (1/r) * 2 * (z_direction_initial - z_direction_new) * (y_direction_initial - y_direction_new);
		float Q_2_2 = 1 - (1/r) * 2 * (z_direction_initial - z_direction_new) * (z_direction_initial - z_direction_new);

		float x_position = ref_secondary.m_position_x;
		float y_position = ref_secondary.m_position_y;
		float z_position = ref_secondary.m_position_z;

		for( int i = 0; i < ref_data_base.m_data_base.at(track_id).m_track.size() - step_id; i++ )
		{
			float x_vec = ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_xvec;
			float y_vec = ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_yvec;
			float z_vec = ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_zvec;

			x_vec_rotated = Q_0_0 * x_vec + Q_0_1 * y_vec + Q_0_2 * z_vec;
			y_vec_rotated = Q_1_0 * x_vec + Q_1_1 * y_vec + Q_1_2 * z_vec;
			z_vec_rotated = Q_2_0 * x_vec + Q_2_1 * y_vec + Q_2_2 * z_vec;

			x_position += x_vec_rotated * ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_step_length;
			y_position += y_vec_rotated * ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_step_length;
			z_position += z_vec_rotated * ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_step_length;

			if(ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_num_secondaries > 0)
			{
				for(int k = 0; k < ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_num_secondaries; k++)
				{
					ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_secondary.at(k).m_position_x = x_position;
					ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_secondary.at(k).m_position_y = y_position;
					ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_secondary.at(k).m_position_z = z_position;

					//Push Stack			
					stack.m_stack.push_back( ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_secondary.at(k) );
				}
			}

			int random_number = rand() % (10); 

			if(random_number > 5)
			{
				dose_grid_index_x = ceil( (x_position) * (resolution/size) );
				dose_grid_index_y = ceil( (y_position) * (resolution/size) );
				dose_grid_index_z = ceil( (z_position) * (resolution/size) );
			}

			if(random_number < 5)
			{
				dose_grid_index_x = floor( (x_position) * (resolution/size) );
				dose_grid_index_y = floor( (y_position) * (resolution/size) );
				dose_grid_index_z = floor( (z_position) * (resolution/size) );
			}

			energy_deposition = ref_data_base.m_data_base.at(track_id).m_track.at(step_id + i).m_delta_energy;

			if(dose_grid_index_x < resolution && dose_grid_index_y < resolution && dose_grid_index_z < resolution && dose_grid_index_x > 0 && dose_grid_index_y > 0 && dose_grid_index_z > 0)
			{		
				ref_dose_grid[dose_grid_index_x][dose_grid_index_y][dose_grid_index_z] += energy_deposition;
			}
		}
	}

	return ref_dose_grid; 
}
Last edited on
1
2
3
4
5
6
7
8
// ref_secondary is initialized to refer to the last element of stack.m_stack		
Secondary &ref_secondary = stack.m_stack.back();
                
// destroy the last element of stack.m_stack
stack.m_stack.pop_back();

// ref_secondary now refers to an object that no longer exists (destroyed)
// accessing the object via ref_secondary would lead to undefined behaviour  
Topic archived. No new replies allowed.