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).

Recommended Answers

All 4 Replies

connections[k++]=&(slot(ii,jj));

This line is suspicious. If connections is a vector, then either it should be resized to have enough space beforehand, or push_back should be called to add a new element each time. Indexing does not resize the vector, that might be why the size is different from how many elements you have tried to add.

connections.push_back(&(slot(ii,jj));
++k;

Vectors only resize if you call the vector::resize() or vector::push_back() methods. If you are accessing/modifying the vector using the subscripting operator (i.e. vectorName[index]) they act just like a regular array and will cause segmentation faults.

More info on vectors in general.

edit:
Wow, 1,000th post. Doesn't seem possible...

thanks. I don't know where I got the impression that they worked the other way.

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?

You thought wrong. The size of a vector changes only if you do something that changes it. Assigning a value to an element is not such a thing.

If you write

vector<int> v;
v[0] = 42;     // No!

you have done something that has undefined behavior, namely tried to assign a value to v[0] when the vector doesn't have any elements.

If you wish to append an element to a vector, you should do something like this:

vector<int> v;
v.push_back(42);   // Yes!

Calling push_back increases the size of the vector by 1, and does so by appending a new element with the given value to the vector.

Calling resize also changes the size, of course; so you could have written

vector<int> v;
v.resize(v.size()+1);
v[v.size()-1] = 42;

but that's more roundabout, in addition to creating a new element, initializing it to 0 (because using resize to increase the size of a vector initializes the new elements), and then setting it to 42.

If you use resize to decrease the size of the vector, it throws away elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.