EXC_BAD_ACCESS

I am writing a simulation that uses two classes that I have defined. One is called Network and one is called Neuron. One of the public members of Network is a pointer to Neuron called allneurons that I want to use as a dynamic array of Neurons. During initialization of a Network, I include the line
allneurons = new Neuron[N]; // initialize an array of N neurons
This is not the problem, but I am not entirely sure that I'm doing it right, so I'm pointing it out in case it is the problem.


The problem I'm having is that during one of the functions included in the Network class called step
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
void Network::step(valarray<double> syn_in) {
	int i;
	double g_AMPA, g_NMDA, g_GABA, g_in=0;
	double stats[6];
	valarray<double> syn_AMPA(N_E), syn_NMDA(N_E), syn_GABA(N_I);
	valarray<double> syns_AMPA, syns_NMDA, syns_GABA, syns_in;
	valarray<double> w_Ei, w_Ii, w_ini(N_in);
	slice iEslice, iIslice, iinslice;
													
	i = int(stats[0]);
	for (i=0; i<N_E; i++) {		// gather local synaptic activations
		allneurons[i].fillwstats(stats);
		syn_AMPA[i] = stats[3];
		syn_NMDA[i] = stats[4];
	}
	for (i=0; i<N_I; i++) {
		allneurons[i+N_E].fillwstats(stats);
		syn_GABA[i] = stats[5];
	}
	
	
	for (i=0; i<N; i++) {				//step over all neurons	
		iEslice =  slice(i*N_E, N_E, 1);
		iIslice =  slice(i*N_I, N_I, 1);
		iinslice = slice(i*N_in, N_in, 1);
		w_Ei = w_E[iEslice];			//initialize weights
		w_ini = w_in[iinslice];
		w_Ii = w_I[iIslice];
		
		//other stuff
	}
}

The first loop (for 0 through N_E) works fine, the second loop works fine until the last iteration (no matter what the maximum value of i is), when I get "EXC_BAD_ACCESS" before the breakpoint that I have before the next loop (that is the error happens on the line of the final } of the second loop).
I will include the code for Neuron::fillwstats as well. It fills an array passed to it with a bunch of the private variables of Neuron.
1
2
3
void Neuron::fillwstats(double arr[6]) {
	arr[0] = V_s; arr[1] = V_d; arr[2] = spike; arr[3] = AMPA; arr[4] = NMDA; arr[5] = GABA;
}
I notice you are declaring valarrays without resizing them. The hidden secret of the valarray, which over the last few years has probably wasted a few months of my life is that they are default constructed with a size of 0, and the assignment operator DOES NOT resize them.

I haven't checked your code in detail, but I imagine if you call resize on all your valarrays the problem will fix itsself.

You code would also probably benefit by passing syn_in as a const reference, unless you modify it in the function.
Last edited on
I give the valarrays the size that I want them to have in their declaration.
Actually, the declaration of valarrays is something that was a little weird. I'm using XCode, and although the valarray documentation here says that the constructor can take two arguments, value of all entries and size in that order, when I was declaring them that way, eg. valarray<double> syn_AMPA(0.0,N_E), they were ending up being declared as size 0 valarrays. When I declare them this way, the debugger says that they have the right size...
actually, kev82, you were right.
Topic archived. No new replies allowed.