LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2022, 04:38 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 354

Rep: Reputation: 6
How do I iterate through a list of pointers c++


This is segmentation faulting and i think its because I am not referencing the pointers correctly to the free_ilf function. how can i iterate through the list of pointers? I think there is an error with free_ilf function loop that is calling itself because it tries to free stuff that might not be a memory address.
Code:
#include <cstdlib>
#include <iostream>
#include <list>

using namespace std;

template<class T>
void Swap(T&a,T&b){
    T temp = a;
    a=b;
    b=temp;
}

class ilf /* inteliigent life form */ {
public:
    string fn;          // First Name
    string mn;          // Middle Name
    string ln;          // Last Name
    long long int a;    // Age
    bool s;             // Sex false Male True Female
    list<ilf> *sib;     // Siblings

    ilf (string fname, string mname, string lname, long long int age, bool sex, list<ilf>*sibling) {
        fn = fname;
        mn = mname;
        ln = lname;
        a = age;
        s = sex;
        sib=sibling;
    }
};

class Morep:public ilf{
public:
    Morep (string fname, string mname, string lname, long long int age, bool sex, list<ilf>*sibling) : ilf (fname, mname, lname, age, sex, sibling){

    }
};

void print_ft(ilf *pilf, string mname) /* Print Family Tree */ {
    if(!pilf)
        return;
    cout<< pilf->fn << ((mname == "NULL") ? "" : ((pilf->s == true) ? " Daughter of "+mname : " Son of "+mname))<<endl;

    if (pilf->sib)
        for(ilf i : *pilf->sib) {
            print_ft(&i, pilf->fn);
        }
}

void free_ilf(ilf *pilf) /* Free Intelligent Life Forms */ {
    if(!pilf)
        return;

    if (pilf->sib)
        for(ilf i : *pilf->sib) {
            free_ilf(&i);
        }
    free(pilf);
}

ilf *MFT = 0;

int main(int argc, char *argv[])
{
    char a='a',b='b';


    // Sons of Mwana
    ilf *Marvin = new Morep("Marvin", "NULL", "NULL", 12499, false, NULL);
    ilf *Ron = new Morep("Ron", "NULL", "NULL", 12498, false, NULL);
    //Daughters of Mwana
    ilf *Jane = new Morep("Jane", "NULL", "NULL", 12497, true, NULL);
    //Mother of Morep
    ilf *Mwana = new Morep("Mwana", "NULL", "NULL", 12500, true, new list<ilf>{*Marvin, *Ron, *Jane});

    MFT=Mwana;

    cout<<a<<" - "<<b<<"\n";
    Swap(a,b);
    cout<<a<<" - "<<b<<"\n";

    print_ft(Mwana, "NULL");

    free_ilf(MFT);
}
 
Old 09-28-2022, 04:46 PM   #2
errigour
Member
 
Registered: May 2009
Posts: 354

Original Poster
Rep: Reputation: 6
Acually i think I found the issue

I was making a list of normal class types not pointers I had to change a few things but to define a list of pointers would be list<ilf*> *sib so that its a pointer to a list of pointers instead of list<ilf> *sib. then i had to reference stuff differently but I got it to work sorry for the troubles.
 
Old 09-28-2022, 04:47 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 354

Original Poster
Rep: Reputation: 6
Here's the working code: This code isn't completely working right tho because now it is not freeing everything that I allocate, i ran it with valgrind.
Code:
#include <cstdlib>
#include <iostream>
#include <list>

using namespace std;

template<class T>
void Swap(T&a,T&b){
    T temp = a;
    a=b;
    b=temp;
}

class ilf /* inteliigent life form */ {
public:
    string fn;          // First Name
    string mn;          // Middle Name
    string ln;          // Last Name
    long long int a;    // Age
    bool s;             // Sex false Male True Female
    list<ilf*> *sib;     // Siblings

    ilf (string fname, string mname, string lname, long long int age, bool sex, list<ilf*>*sibling) {
        fn = fname;
        mn = mname;
        ln = lname;
        a = age;
        s = sex;
        sib=sibling;
    }
};

class Morep:public ilf{
public:
    Morep (string fname, string mname, string lname, long long int age, bool sex, list<ilf*>*sibling) : ilf (fname, mname, lname, age, sex, sibling){

    }
};

void print_ft(ilf *pilf, string mname) /* Print Family Tree */ {
    if(!pilf)
        return;
    cout<< pilf->fn << ((mname == "NULL") ? "" : ((pilf->s == true) ? " Daughter of "+mname : " Son of "+mname))<<endl;

    if (pilf->sib)
        for(ilf *i : *pilf->sib) {
            print_ft(i, pilf->fn);
        }
}

void free_ilf(ilf *pilf) /* Free Intelligent Life Forms */ {
    if(!pilf)
        return;

    if (pilf->sib)
        for(ilf *i : *pilf->sib) {
            free_ilf(i);
        }
    free(pilf);
}

ilf *MFT = 0;

int main(int argc, char *argv[])
{
    char a='a',b='b';


    // Sons of Mwana
    ilf *Marvin = new Morep("Marvin", "NULL", "NULL", 12499, false, NULL);
    ilf *Ron = new Morep("Ron", "NULL", "NULL", 12498, false, NULL);
    //Daughters of Mwana
    ilf *Jane = new Morep("Jane", "NULL", "NULL", 12497, true, NULL);
    //Mother of Morep
    ilf *Mwana = new Morep("Mwana", "NULL", "NULL", 12500, true, new list<ilf*>{Marvin, Ron, Jane});

    MFT=Mwana;

    cout<<a<<" - "<<b<<"\n";
    Swap(a,b);
    cout<<a<<" - "<<b<<"\n";

    print_ft(Mwana, "NULL");

    free_ilf(MFT);
}

Last edited by errigour; 09-28-2022 at 05:32 PM.
 
Old 09-28-2022, 04:58 PM   #4
errigour
Member
 
Registered: May 2009
Posts: 354

Original Poster
Rep: Reputation: 6
is there a way for me to declare
Code:
list<ilf*> *sib
like a template so it will take any class?
Code:
list<(any class)*> *sib
?
 
Old 09-28-2022, 05:43 PM   #5
errigour
Member
 
Registered: May 2009
Posts: 354

Original Poster
Rep: Reputation: 6
Found the leak, fixed it, this is working code:
Code:
#include <cstdlib>
#include <iostream>
#include <list>

using namespace std;

template<class T>
void Swap(T&a,T&b){
    T temp = a;
    a=b;
    b=temp;
}

class ilf /* inteliigent life form */ {
public:
    string fn;          // First Name
    string mn;          // Middle Name
    string ln;          // Last Name
    long long int a;    // Age
    bool s;             // Sex false Male True Female
    list<ilf*> *sib;     // Siblings

    ilf (string fname, string mname, string lname, long long int age, bool sex, list<ilf*>*sibling) {
        fn = fname;
        mn = mname;
        ln = lname;
        a = age;
        s = sex;
        sib=sibling;
    }
};

void print_ft(ilf *pilf, string mname) /* Print Family Tree */ {
    if(!pilf)
        return;
    cout<< pilf->fn << ((mname == "NULL") ? "" : ((pilf->s == true) ? " Daughter of "+mname : " Son of "+mname))<<endl;

    if (pilf->sib)
        for(ilf *i : *pilf->sib) {
            print_ft(i, pilf->fn);
        }
}

void free_ilf(ilf *pilf) /* Free Intelligent Life Forms */ {
    if(!pilf)
        return;

    if (pilf->sib) {
        for(ilf *i : *pilf->sib) {
            free_ilf(i);
        }
        delete(pilf->sib);
    }
    delete(pilf);
}

ilf *MFT = 0; /* Morep Family Tree */

int main(int argc, char *argv[])
{
    char a='a',b='b';


    // Sons of Mwana
    ilf *Marvin = new ilf("Marvin", "NULL", "NULL", 12499, false, NULL);
    ilf *Ron = new ilf("Ron", "NULL", "NULL", 12498, false, NULL);
    //Daughters of Mwana
    ilf *Jane = new ilf("Jane", "NULL", "NULL", 12497, true, NULL);
    //Mother of ilf
    ilf *Mwana = new ilf("Mwana", "NULL", "NULL", 12500, true, new list<ilf*>{Marvin, Ron, Jane});

    MFT=Mwana;

    cout<<a<<" - "<<b<<"\n";
    Swap(a,b);
    cout<<a<<" - "<<b<<"\n";

    print_ft(MFT, "NULL");

    free_ilf(MFT);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash.iterate through same file names with different extensions zaza1851983 Linux - General 5 06-01-2012 06:46 AM
need help to iterate through PID's mcnkevin Programming 7 11-24-2007 06:08 PM
Iterate through regular expression results in Perl rose_bud4201 Programming 1 04-14-2006 02:47 PM
Bash: Iterate through $@ (easy++) beatnik007 Programming 2 04-03-2004 02:39 AM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 10:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration