object is getting scrambled, but why? plz help!

B

brulsmurf

Ok iam despered, in Main of this code the object 'two' is looking as
should be when i print it, but the exact same code in procedure
"duTest" makes the object
filled up with additional strange values. I played with this code for
hours but i cant find the problem. Plz help me


#include "genetic.h"
#include "neuron.h"
#include "network.h"
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

void duTest()
{
DNA one(0);
UniversalNeuron two;

one.addAllel(4);
one.addAllel(4);
one.addAllel(1);
one.addAllel(3);
one.addAllel(3); //filling up one.

two.insertDNA(one);
two.show(); // Why dont it work??
}

int main()
{
int dummie;
DNA one(0);
UniversalNeuron two;

one.addAllel(4); // filling up one.
one.addAllel(4);
one.addAllel(1);
one.addAllel(3);
one.addAllel(3);

cout << "\nHere duTest routine\n";
duTest(); // doesnt work <-- inside here two gets filled
//with the right values, but also with some strange values

two.insertDNA(one); // works
two.show(); // ^^ <--this works fine,two is not
//filled with strange values

scanf("%d",&dummie); // press key..
return(1);
}


////// paste from other file VV
class UniversalNeuron
{
public: //
UniversalNeuron();
UniversalNeuron(int nS ,int gS, int dS, int daS);//
~UniversalNeuron();
UniversalNeuron(UniversalNeuron const &other);
void operator=(UniversalNeuron const &other);
void insertDNA(DNA &d); // <-- maybe interesting for
// solving problem ???
void show();

etc etc
 
R

roberth+news

(e-mail address removed) wrote:
| Ok iam despered, in Main of this code the object 'two' is looking as
| should be when i print it, but the exact same code in procedure
| "duTest" makes the object
| filled up with additional strange values. I played with this code for
| hours but i cant find the problem. Plz help me
|
|
| #include "genetic.h"
| #include "neuron.h"
| #include "network.h"

Neither of these are standard headers, and you haven't posted them.

| #include <iostream>
| #include <algorithm>
| #include <string>
|
| using namespace std;

Please don't do that.

| void duTest()
| {
| DNA one(0);

Unknown identifier DNA.

| UniversalNeuron two;

I'll assume UniversalNeuron is a class because you said so further down.

| one.addAllel(4);
| one.addAllel(4);
| one.addAllel(1);
| one.addAllel(3);
| one.addAllel(3); //filling up one.

What does addAllel do?

| two.insertDNA(one);

What does insertDNA do?

| two.show(); // Why dont it work??

How should I know why it doesn't work, when you haven't posted any
relevant code.

| }
|
| int main()
| {
| int dummie;
| DNA one(0);
| UniversalNeuron two;
|
| one.addAllel(4); // filling up one.
| one.addAllel(4);
| one.addAllel(1);
| one.addAllel(3);
| one.addAllel(3);
|
| cout << "\nHere duTest routine\n";
| duTest(); // doesnt work <-- inside here two gets filled
| //with the right values, but also with some strange values
|
| two.insertDNA(one); // works
| two.show(); // ^^ <--this works fine,two is not
| //filled with strange values
|
| scanf("%d",&dummie); // press key..

You haven't #included any header for scanf.

| return(1);

Consider returning EXIT_SUCCESS or 0.

| }
|
|
| ////// paste from other file VV
| class UniversalNeuron
| {
| public: //
| UniversalNeuron();
| UniversalNeuron(int nS ,int gS, int dS, int daS);//
| ~UniversalNeuron();
| UniversalNeuron(UniversalNeuron const &other);
| void operator=(UniversalNeuron const &other);
| void insertDNA(DNA &d); // <-- maybe interesting for
| // solving problem ???

Maybe.

| void show();
|
| etc etc

Please post a minimal example that reproduces the error, and we might be
able to help you.
 
D

Donovan Rebbechi

Ok iam despered, in Main of this code the object 'two' is looking as
should be when i print it, but the exact same code in procedure
"duTest" makes the object

two is neither declared nor defined in that code. Neither have you posted
any of the class methods that are possible causes of this problem. If
you're not using pointers anywhere, I'm guessing that one of your variables
isn't initialized properly.

Cheers,
 
B

brulsmurf

I guess this is the most relevant code, its quite big sorry for that .
And really tnx for your effort ^^


void DNA::addAllel(int a)
{
int *tempDNA;
int tempDNASize = size;
tempDNA = new int[tempDNASize];
for (int i = 0; i < tempDNASize; i++) // fout in deze for lus.
tempDNA = dnaString;
delete [] dnaString;
dnaString = new int[++size];
for (int i = 0; i < tempDNASize; i++)
dnaString = tempDNA;
dnaString[tempDNASize] = a;
delete [] tempDNA;
}

class DNA
{
public:
DNA();
DNA(int sz);
DNA(DNA const &other);
~DNA();
int getAllel(int p);
int getSize();
void operator=(DNA const &other);
void showDNA();
void mutate(float allelFlipProb, float lengthChangeProb);
void mutateTwee(float allelFlipProb, float lengthChangeProb);
void crossover(float crossoverProb, const DNA &other);
void addAllel(int a); // ads allel on end.
int getKarma();
void putKarma(int k);
private:
void copy(DNA const &other);
void destroy(void);

int karma; // opzoeken.
int size;
int *dnaString;
};

class UniversalNeuron
{
public: // sommige procedures moeten nu private worden.
UniversalNeuron();
UniversalNeuron(int nS ,int gS, int dS, int daS);// neighbourhood,
grid, detEm, detEmAll.
~UniversalNeuron();
UniversalNeuron(UniversalNeuron const &other);
void operator=(UniversalNeuron const &other);
void insertDNA(DNA &d); // werkt zo?
void show();
void getOut();
void putIn();
void process();
void putSubneuron(int t, int s);
void getEmiterAcces(int s, Emitter *neighbour);// zoiets..

int getDetEm(); // moeten nog VV
void putDetected(int s, float sz ,int sub); // (spec , size ,
subneuron)
void putSpike (float sp, int sub); // (spike, subneuron)


private:
void copy(UniversalNeuron const &other);
void destroy(void);

float nFunc(float a, float b, float x);
void nfaAdjust(float a);
void nfbAdjust(float a);
void copyAdjust(float a);
void destructAdjust(float a);
void staticOne();
void staticTwo();
void dynamicOne();
void dynamicTwo(int i);
void dynamicThree(int i, int j , float in);
void dynamicThreeLocal(int i, int j, float in , int sN);
void dynamicFour(int i);
void dynamicFive();

Manifestation *blueDetectors;
Manifestation *blueEmitters;

SubNeuron *subParts; // bij een 3d grid heeft een neuron 6 , 18 of
26(!) buren.
int neighbourhood; // soort neighbourhood
float nFuncA;// parameters van Neuron functie.
float nFuncB;
float inbox;
float outbox;
float copyTrigger; // bepaald of copieren
float destructTrigger; // bepaald of destructen

int detEmAll; // alle soorten emmiters of detectors
int detEm; // detectors or emitters on each subneuron
int gridSize; // size of grids.
ConditionCategory **ifCondition;
float **thenCondition;

int alive;
};

void UniversalNeuron::insertDNA(DNA &d )
{
int effectiveSize;
effectiveSize = d.getSize() - (d.getSize()%5); // meervoud van 5.

cout << "\ninsertDNA ffsize : " << effectiveSize << "\n"; // debug
for (int j = 0; j < effectiveSize; j++) //
cout << d.getAllel(j) <<"-"; //
cout <<"\n"; //

for (int i = 0; i < effectiveSize; i+=5) // 5 na lus toegevoegd?
{
cout <<d.getAllel(i)<<" "<<d.getAllel(i+1) <<" "<<d.getAllel(i+3)
<<" "<<d.getAllel(i+4) <<" "<<d.getAllel(i+2)<< " ; ";

ifCondition[d.getAllel(i)][d.getAllel(i+1)].addCondition(d.getAllel(i+3),d.getAllel(i+4),d.getAllel(i+2));
}
cout << "\n";
}
 
B

brulsmurf

I will try to make an minimal example that does the same and post that
one,
All Tnxs for your effort and advice ^^
 
K

Karl Heinz Buchegger

I guess this is the most relevant code, its quite big sorry for that .
And really tnx for your effort ^^

void DNA::addAllel(int a)
{
int *tempDNA;
int tempDNASize = size;
tempDNA = new int[tempDNASize];
for (int i = 0; i < tempDNASize; i++) // fout in deze for lus.
tempDNA = dnaString;
delete [] dnaString;
dnaString = new int[++size];
for (int i = 0; i < tempDNASize; i++)
dnaString = tempDNA;
dnaString[tempDNASize] = a;
delete [] tempDNA;
}


Why all that complicated memory management?
You already have a dynamic growing array at your finger
tips, just use it!

class DNA
{
public:

...

int karma; // opzoeken.
// no longer needed int size;
std::vector< int > dnaString;
};

and your addAllel() function shrinks down to:

void DNA::addAllel(int a)
{
dnaString.push_back( a );
}

and that's the benefit:
* it works out of the box
* you don't need a custom copy constructor any
longer (same for: destructor, assignment operator)
* it will be at or very near the same speed then your home-
made dynamic array. But most likely it will be faster then
your home made array
* lots of complicated and error prone code vanishes into thin air.
* Did I already mention, that std::vector works out of the box?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top