So, in advance, I have to admit that this is not quite as "good" a question as what is otherwise found here. It's not about a weird error, or something I can't figure out to do. I just get weird output. I've spent around 2 days simplifying the code, trying to pin down the error. I really can't.
The project concerns simulating neural networks, so the two classes in the script are neurons and networks. The neurons need to know which other neurons they are linked to, so one of their properties is an array of pointers to other neurons in the network. When I was checking whether the networking was working alright, I noticed that it was not. The neurons have a public function called "introduction()" which causes them to write their address in the grid and the address of all their connections.
Since this is the simplified version, all neurons talk to all neurons, including themselves. An address of (100,100) means that the neuron hasn't been placed on the grid yet.
I will comment more at the bottom.
the "pre-amble":
using namespace std;
#define DEBUG
#include <cstdlib>
#ifndef DEBUG
#define SHOW(x)
#define PRINT(x)
#else
#include <iostream>
#include <fstream>
#include <iomanip>
ofstream output;
//output.open ("output.txt");
#define SHOW(x) \
output << #x << std::setprecision(4) << ":\t" << x << std::endl;
#define PRINT(x) \
output << x << std::endl;
#endif
The neuron-class:
class neuron
{
public:
int threshold;
bool type;
neuron** connections;
int nconnect;
int i;
int j;
neuron();
neuron(neuron** incconnctions, int incnconnect);
~neuron();
void introduction();
};
neuron::neuron(): i(100),j(100)
{
//booh
}
neuron::neuron(neuron** incconnctions, int incnconnect):
nconnect(incnconnect)
{
PRINT("Constructing neuron");
connections=new neuron*[nconnect];
for (int i=0;i<nconnect; i++)
{
connections[i]=incconnctions[i];
}
PRINT("Connections:");
for (int ii=0;ii<nconnect;ii++)
{
output<< (*connections[ii]).i<<","<<(*connections[ii]).j<<"\n";
}
}
neuron::~neuron()
{
// output<< "Destroying neuron " << i << "," << j <<"\n";
delete[] connections;
}
void neuron::introduction()
{
output<<"My name is "<< i <<"," <<j<<"\n";
PRINT("I am connected to:");
for (int ii=0;ii<nconnect;ii++)
{
output<< (*connections[ii]).i<<","<<(*connections[ii]).j<<"\n";
}
}
The network class:
class network
{
public:
int dimensions[2];
int nneurons;
network(int* dimensions);
~network();
neuron* neurons;
neuron** connections;
};
network::network(int* incdimensions)
{
for (int i=0;i<2;i++)
{
dimensions[i]=incdimensions[i];
}
nneurons=dimensions[0]*dimensions[1];
neurons=new neuron[nneurons];
connections = new neuron*[nneurons];
for (int j=0;j<dimensions[1];j++)
{
for (int i=0;i<dimensions[0];i++)
{
for (int k=0;k<nneurons;k++)
{
connections[k]=&(neurons[k]);
}
neurons[i+ j*dimensions[0]]=neuron(connections, nneurons); //counting down along the columns
neurons[i+ j*dimensions[0]].i=i;
neurons[i+ j*dimensions[0]].j=j;
PRINT("Asking directly:");
for (int k=0;k<neurons[i+ j*dimensions[0]].nconnect;k++)
{
SHOW((*(neurons[i+ j*dimensions[0]].connections[k])).i);
SHOW((*(neurons[i+ j*dimensions[0]].connections[k])).j);
}
neurons[i+ j*dimensions[0]].introduction();
PRINT("Post-intro:");
for (int ii=0;ii<nneurons;ii++)
{
output<< (*connections[ii]).i<<","<<(*connections[ii]).j<<"\n";
}
}
}
delete[] connections;
connections=0;
PRINT("Leaving network constructor");
}
network::~network()
{
delete[] neurons;
}
Main:
int main()
{
output.open ("output.txt");
int dimensions[]={2,2}; //#rows,#columns
network net(dimensions);
output.close();
return 1;
}
When run, the program writes quite a lot, since there are several loops involved. However, it's pretty much the same for every loop, and it suffices to simply look at the last iteration:
Constructing neuron
Connections:
0,0
1,0
0,1
100,100
Asking directly:
(*(neurons[i+ j*dimensions[0]].connections[k])).i: 0
(*(neurons[i+ j*dimensions[0]].connections[k])).j: 0
(*(neurons[i+ j*dimensions[0]].connections[k])).i: 4066937
(*(neurons[i+ j*dimensions[0]].connections[k])).j: 0
(*(neurons[i+ j*dimensions[0]].connections[k])).i: 0
(*(neurons[i+ j*dimensions[0]].connections[k])).j: 1
(*(neurons[i+ j*dimensions[0]].connections[k])).i: 1
(*(neurons[i+ j*dimensions[0]].connections[k])).j: 1
My name is 1,1
I am connected to:
0,0
4066937,0
0,1
1,1
Post-intro:
0,0
1,0
0,1
1,1
Leaving network constructor
As can be seen (especially comparing with the code above), the address of the second neuron, instead of being (1,0) which is the correct value, is read, by the other neurons, as (4066937,0). I have no idea why. 4066937 is of course a somewhat random value, which will change for every run, but it is always the first coordinate of the second neuron that is wrong, no matter how many neurons I have in my network (though I haven't checked beyond 16). I also see that the address is read correctly when the "connections"-array has just been loaded into the neuron, but when I have exited the neuron-constructor and ask again, I get this same, wrong value.
I can not for the life of me see what is special about entrance 1 - I have even done a textsearch for all 1's to see if any were in some weird places.
can anyone spot it? please. I know this post is really long, and I would myself have preferred it shorther, since I might have been able to spot the problem myself then, but these seem to be the necessary ingredients.
Neuron (1,0) introduces itself correctly (as 1,0).
Thanks for reading all of it =)