So, due to my last question, http://www.daniweb.com/forums/thread303487.html, I am becoming acquainted with the vector template. However, I clearly am not using the commands correctly. Below is a section of code which, eventually, leads to a segmentation fault later in the program.

First an introduction of the variables:

```
using namespace std;
int dimensions[]; //the neurons are on a grid of size dimensions[0] X dimensions[1]
int k; //incremented each time an element in connections is assigned an address.
vector<neuron*> connection; //a container of pointers to neurons, so my individual neuron knows who it's neighbours are
```

```
int k=0;
for (int ii=0;ii<dimensions[0];ii++) {
for (int jj=0;jj<dimensions[1];jj++) {
if (!(ii==i && jj==j))
{
double dist = distance(ii,jj,i,j);
if (dist>0 && iflucky(p[(int) ceil(dist)]))
{
mexPrintf("Adding pointer to (%i,%i): %p\n",ii,jj,&(slot(ii,jj)));
connections[k++]=&(slot(ii,jj));
}
}
}
}
mexPrintf("Listing connections:\nk: %i\n",k);
mexPrintf("Before resize:\n length: %i\n",connections.size());
for (int i=0;i<k;i++)
{
mexPrintf("connections[%i]: %p\n",i,connections[i]);
}
connections.resize(k);
mexPrintf("After resize:\n length: %i\n",connections.size());
for (int i=0;i<k;i++)
{
mexPrintf("connections[%i]: %p\n",i,connections[i]);
}
```

"mexPrintf()" is a printing funciton necessary to run the code through matlab (at least if I want it to print). It works exactly like printf();

As usual, the output it quite long and repetitive. However, an example of a repetition in which the code "malfunctions":

```
Adding pointer to (1,1): 053D11F0
Adding pointer to (1,3): 053D13A8
Adding pointer to (3,2): 053D1324
Listing connections:
k: 3
Before resize:
length: 1
connections[0]: 053D11F0
connections[1]: 053D13A8
connections[2]: 053D1324
After resize:
length: 3
connections[0]: 053D11F0
connections[1]: 00000000
connections[2]: 00000000
```

So, what is puzzling me here is how the length can be shorther than the amount of elements in the vector? I thought it was supposed to resize automatically? I can see that lengthening the vector would make it delete what was there before. the resize command is there to make sure I don't carry around very long vectors of zeros. my networks will eventually become very large, and the average number of connections will be much shorter than the possible maximum, so it is an important concern. Also, no matter what, my neuron needs to know when it has spoken to all its neighbours.

a lecture is very welcome =)

(and yes, I did do an extensive search of vector-related threads before writing this, but no one seemed to have had my exact problem).