I'm not sure where my error is coming from because it complies with no errors. The idea behind this code is that i have a set of functions that fill arrays and vectors and I want to use a for loop to run the function multiple times. I can loop it so it runs 2 times but anymore than that it just crashes. After removing each function and then adding in one at a time I found that the lines 24-25 (about incrementing i) are causing the code to crash.Here is the code
I don't know if this is your problem, but in CFifo(), when you have cleared fifo_vec, you should exit the for loop, because the next iteration will check position t of a vector that is empty
Good point, You know this part of my code has not been used so I dont think it is crashing but it is something i will have to fix. Do you would know how to clear a single vector that is inside a vector, because in CFifo if the the 'if' statement clears all the vectors not just the one where the 10th element is 1.
But as far as the original problem, i've been playing around and its definitely lines 24 and 25. The i doesnt seem to increment, i did something likeif(i==3) cout<<"hello"<<endl; and it never said hello, i never got passed 1. In fact if i set i to any other value other than 0 or 1 the program crashes.
void CReadout::loadFifo_vec(int i){
fifo_vec.resize(channel.size() );
for (int t=0;t<channel.size();t++) {
if (fifo_vec[t][10]!=1){ // You've resized fifo_vec, but the vectors inside your vector are empty..
if ( getChannel(t)==1){ // 10 is out of bounds anyhow, your index would be 0-9.
fifo_vec[t][i]=1;
}
}
}
}
void CReadout::Buffer(int g){
for (int t=0;t<channel.size();t++) {
for (int j=0;j<i;j++){ // Where does i come from?
if(fifo_vec[t][j]==1) {
buffer_ary[g]=1;
g++;
}
}
}
cout<<g<<endl;
}
Why do you resize channel in the constructor, then again inside AssignRnd()? I recommend an initializer list for your constructor, use channel.size() instead of passing nchannel around everywhere, and using iterators for your containers wouldn't go astray either.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
#include <map>
#include <utility>
#include <vector>
#include <string>
usingnamespace std;
CReadout::CReadout( int nchannel, double hitprob ) {
channel.resize( nchannel );
fifo_vec.resize(channel.size() );
for( int m = 0; m < 10; m ++ ) {
AssignRnd( hitprob );
loadFifo_vec( i );
Buffer( g, i );
CFifo( i );
if (i==10) i=0;
else i++;
}
}
CReadout::~CReadout( ) {
}
void CReadout::AssignRnd(double hitprob){
double random;
for( int a=0; a<channel.size(); a++) {
random = rand() % 10000;
double value = random/10000;
if( value < hitprob ) channel[a]=1 ;
else channel[a]=0;
}
}
void CReadout::loadFifo_vec(int i){
for (int t=0;t<channel.size();t++) {
if (fifo_vec[t][9]!=1){ //full statement and set size(kinda the same thing)
if ( getChannel(t)==1){
fifo_vec[t][i]=1;
}
}
}
}
void CReadout::Buffer(int g,int i){
for (int t=0;t<channel.size();t++) {
for (int j=0;j<i;j++){
if(fifo_vec[t][j]==1) {
buffer_ary[g]=1;
g++;
}
}
}
}
void CReadout::CFifo(int i){
for(int t=0;t<channel.size();t++){
if (fifo_vec[t][9]==1){ //////////////////////////////////////////////////adjust fifo size
fifo_vec.clear();
}
}
}
Still two things I dont understand, how do i resize the vector which is nested in fifo_vec and why my iterator i++ is not working. For some reason if the value of i goes above 1 the code crashes. I feel like I am not initializing i correctly.