any suggestions please .. facing an error

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

class inputlayer
{
public:
inputlayer() ;
~inputlayer() ;
void initialize() ;
inline double random(){ return (rand()/double(RAND_MAX)); } ;
double findnorm(int i) ;
void simulate(SonarProxy &sp) ;
double getactivation() ;
neuron * ilayer() ;
liquid * llayer() ;
readout * rlayer() ;

private:
double activation ;
double inorm, ibeta ;
neuron * h_inp_layer ;
liquid * liq ;
readout * readout_layer ;
};

constructor of inputlayer in the inputlayer.cpp file is below

inputlayer::inputlayer()
{

liq = new liquid() ;
liq->initialize() ;

readout_layer = new readout() ;
readout_layer->initialize(liq) ;

h_inp_layer = new neuron[NEU_INP_LAYER] ; // THE IMPORTANT LINE
ibeta = 0.7*pow(LIQUID_NEURONS,(1/NEU_INP_LAYER));
activation = 0.8 ;
}

the function that returns the pointer to the array of struct is below
neuron * inputlayer::ilayer()
{
return h_inp_layer ;
}



calling from another file is below


inputlayer ** ptr1 = NULL ;
inputlayer ** ptr2 = NULL ;

inputlayer * pyptr = NULL ;
inputlayer * pdptr = NULL ;

inputlayer ** PREY_GEN_HOLDER[MAX_GEN] ;
inputlayer ** PRED_GEN_HOLDER[MAX_GEN] ;

for(int GEN = 0; GEN < MAX_GEN; GEN++) //Loop for each generation
{
ptr1 = new inputlayer * [MAX_POP_SIZE] ;
ptr2 = new inputlayer * [MAX_POP_SIZE] ;

PREY_GEN_HOLDER[GEN] = ptr1 ; //Pointer to an array of 10 prey
PRED_GEN_HOLDER[GEN] = ptr2 ; //Pointer to an array of 10 predator

pyptr = ptr1[0] ;
pdptr = ptr2[0] ;

for(int POP = 0; POP < MAX_POP_SIZE; POP++)
{
pyptr = new inputlayer() ;
pyptr->initialize() ;
pyptr++ ;

pdptr = new inputlayer() ;
pdptr->initialize() ;
pdptr++ ;
}
}

Above code is fine .. just for reference
Code below gives an error specially the line marked as ERRORONEOUS

for(int gen = 0; gen < MAX_GEN; gen++)
{
ptr1 = PREY_GEN_HOLDER[gen] ;
ptr2 = PRED_GEN_HOLDER[gen] ;

pyptr = ptr1[0] ; //point to full prey population of gen generation
pdptr = ptr2[0] ; //point to full pred population of gen generation

pd_fit = 0 ;
for(int pred = 0; pred < MAX_POP_SIZE; pred++)
{
py_fit = 0 ;
for(int prey = 0; prey < MAX_POP_SIZE; prey++)
{
neuron * p = pyptr->ilayer() ; //ERRONEOUS

if(py_fit > py_bestfit)
{
py_bestfit = py_fit ;
PREYBEST[gen] = pyptr ;
}
pyptr++ ;
}

if(pd_fit > pd_bestfit)
{
pd_bestfit = pd_fit ;
PREDBEST[gen] = pdptr ;
}
pdptr++ ;
}
}

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.
Last edited on
[code] "Please use code tags" [/code]
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?
Last edited on
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.

I am trying to implement a neural network
Last edited on
You never fill the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.
Topic archived. No new replies allowed.