-2

I have two funcions

int zapiszGenotyp(genotyp *p, int dlugoscGenotypu, int genDoWpisania)
{
    if (!p)
    {
        genotyp *nowy = new genotyp;
        nowy->pNext = 0;
        p = nowy;
        nowy->gen = genDoWpisania;
        dlugoscGenotypu++;
    }
    else if (p->pNext == 0)
    {
        genotyp *nowy = new genotyp;
        nowy->pNext = 0;
        p->pNext = nowy;
        nowy->gen = genDoWpisania;
        dlugoscGenotypu++;
    }
    else
        zapiszGenotyp(p->pNext, dlugoscGenotypu, genDoWpisania);

    return dlugoscGenotypu;
}





void stworzListeOsobnikow(osobnicy *& p, ifstream & input)
{
    while (input)
    {
        if (p==0)
        {
            p = new osobnicy;
            string linia;
            getline(input, linia);
            istringstream strumien(linia);
            int gen;

            while (strumien >> gen)
            {
                p->dlugoscDNA = zapiszGenotyp(p->pGeny, 0, gen);
            }


            p->pNext = 0;

            double(*wybraneDopasowanie)(genotyp*);
            wybraneDopasowanie = dopasowanie1;
            p->dopasowanie = wybraneDopasowanie(p->pGeny);

        }
        else if (p->pNext == 0)
        {

        osobnicy *nowy = new osobnicy;
        string linia;
        getline(input, linia);
        istringstream strumien(linia);
        int gen;

        while (strumien >> gen)
        {
            nowy->dlugoscDNA=zapiszGenotyp(nowy->pGeny, 0, gen);
        }

        nowy->pNext = 0;
        p->pNext = nowy;

        double(*wybraneDopasowanie)(genotyp*);
        wybraneDopasowanie = dopasowanie1;
        p->dopasowanie = wybraneDopasowanie(p->pGeny);

    }
    else
        stworzListeOsobnikow(p->pNext, input);
}

And they use following structures

struct genotyp
{   
    genotyp * pNext;        
    int gen;                
};

struct osobnicy
{

    osobnicy * pNext;   
    genotyp * pGeny;        
    int dlugoscDNA;         
    double dopasowanie;     
};

When I try to run then code it stops on when the stworzListeOsobnikow() runs the zapiszGenotyp(). I've realised that pointer passed to zapiszGenotyp() as genotyp *p is always CDCDCDCD but i have no idea what it means. Just using p->pNext in zapiszGenotyp() crashes the program. Could anyone explain to me what is this CDCDCDCD pointer, and what seem to be wrong with my code?

Jarek N
  • 55
  • 9
  • Can you sort out the format of the code. BTW CDCDCDC,.. means something has not been initialiased – Ed Heal Jan 21 '17 at 20:39

1 Answers1

1

You never initialize any of your pointers, and the behavior when attempting to dereference an uninitialized pointer (or read from any uninitialized variable) is undefined. Many compilers will fill uninitialized memory with some distinctive pattern (like 0xCDCDCDCD) when you compile in debug mode to make these sorts of errors more apparent.

You need to add constructors to your structs that initialize their members to some initial value or ensure their members are initialized in some other way.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • So, just making it osobnicy * pNext=0; genotyp * pGeny=0; Will make it work? – Jarek N Jan 21 '17 at 21:08
  • That will work if you are using C++11, though you should prefer `nullptr` over `0` for initializing pointers. You should also initialize `genotyp::pNext` and probably the pointer you're passing in to `stworzListeOsobnikow` as `p` (you don't show where you declare that pointer, so I can't tell you if it's initialized or not). – Miles Budnek Jan 21 '17 at 21:12