I have written a class of my own named inputlayer. inside this class i have a struct named neuron. In the constructor of inputlayer i am creating an array of the struct dynamically and storing the pointer in a private variable of the type of the struct. I also have a public function that return to me this pointer to the array of struct. In another file i create am calling this function on a pointer variable of inputlayer to get the pointer to the array of struct. The code compiles but it gives me a segmentation fault at runtime. Can anyone please suggest me what can i do to remove the error. Code is below
My class inputlayer.h is below
struct neuron //trying out with a structure
{
liqneuron * inp_to_liq[LIQUID_NEURONS] ;
double conn[LIQUID_NEURONS] ;
};
Can anyone please see the code and try to suggest what can be an alternative implementation wherein i can use the pointer to the neuron structure that i have defined. I need to do these dynamically using the new operator.
I need to do these dynamically using the new operator.
No, you don't. That is (probably) the cause of your problems. Take a look at std::vector
readout_layer = new readout() ; Really, tell me what advantages that gives you. You better declare an object readout readout_layer; and now you don't have to worry about leaks or invalid address.
1 2 3 4 5 6 7 8 9 10
for(int POP = 0; POP < MAX_POP_SIZE; POP++)
{
pyptr = new inputlayer() ;
pyptr->initialize() ;
pyptr++ ; //leak (and the pointer becomes invalid)
pdptr = new inputlayer() ;
pdptr->initialize() ;
pdptr++ ;
}
By the way, I'm curious about your program ¿care to share more context?
Thank u for the reply but dont u think the moment the pointer crosses over the for loop exits and the pointer is no longer getting used. Should it create any problem ?
yes off course i could use the vector implementation too. probably i may need use it.
Btw i am facing the error in the below line and as u can see i am reinitializing pyptr to the beginning of the array.
neuron * p = pyptr->ilayer() ; //ERRONEOUS [code]
segmentation fault occurs during runtime for the above line. do you think that eliminating the dynamic creation of the arrays would solve it.
ptr1 = new inputlayer * [MAX_POP_SIZE] ;
ptr2 = new inputlayer * [MAX_POP_SIZE] ;
PREY_GEN_HOLDER[GEN] = ptr1 ; //Pointer to an array of pointers
PRED_GEN_HOLDER[GEN] = ptr2 ;
//Code that doesn't modify the ptr arrays
for(int gen = 0; gen < MAX_GEN; gen++)
{
ptr1 = PREY_GEN_HOLDER[gen] ;
ptr2 = PRED_GEN_HOLDER[gen] ;
pyptr = ptr1[0] ; //invalid pointer
pdptr = ptr2[0] ;
With vector
1 2 3 4 5 6
vector< vector<inputlayer> > prey(MAX_GEN);
for(size_t K=0; K<MAX_GEN; ++K){
prey[K] = vector<inputlayer>(MAX_POP_SIZE); //you could do prey(MAX_GEN, vector<inputlayer>(MAX_POP_SIZE)) but I think this is more clear
for(size_t L=0; L<MAX_POP_SIZE; ++L)
prey[K][L].initialize();
}
By the way ibeta = 0.7*pow(LIQUID_NEURONS,(1/NEU_INP_LAYER)); I suppose that 'NEU_INP_LAYER' is an integer, the division will be truncated to 0
¿Are you trainning the network with a genetic algorithm?
Yes i will be training the network with a genetic algorithm.
I have redone my code again as said by you with vectors. Thank You for you suggestion regarding this but i am facing an out of range error which i am not able to understand. Could you help me regarding this ?
I used the gdb to debug and found that the exception is getting raised when i am trying to push_back to a private vector element from the constructor of my class.
I would like to see the code.
Besides the operator[] you've got the at() method. It will raise an exception if you try to access an out of range element.