Question

i have some memory related problem in my program.I started to debuggin it. What i find out is: After check1 i have error:Floating exception(core Dumbed) for 5 in czynnikipierwsze, segmention fault for 10 number and bad_alloc when uncommenting the last part of the code. Any ideas what could be the possible reason for this(with vector p = ....)

#include <iostream>
#include <cctype>
#include <vector>


using namespace std;
vector<int> CzynnikiPierwsze(int);


class NieprawidlowaDana                           //wyjatki
    {};
class SpozaZakresu
    {};



class RozkladLiczby{
    private:
        int *tab;
    public:
        RozkladLiczby(int);                  //konstruktor
        vector<int> CzynnikiPierwsze(int); //metoda
        ~RozkladLiczby();                  //destruktor
};  


/////////////////BODY of the CLASS/////////////////////////////////////


RozkladLiczby::~RozkladLiczby()         //destruktor
{}

RozkladLiczby::RozkladLiczby(int n){
    int* tab = new int[n+1];
    int i,j;

    for( i=0;i<=n;i++)
        tab[i]=0;                  //zerujemy tablice

    for( i=2;i<=n;i+=2)
        tab[i]=2;                  //zajmujemy sie liczbami parzystymi

    for(i=3; i<=n;i+=2)
        for(j=i;j<=n;j+=i)         //sito erastotesa
            if(tab[j]==0)
                tab[j]=i;


    for(int i=0;i<=10;i++)
        cout<<tab[i]<<endl;         

}

   vector<int> RozkladLiczby::CzynnikiPierwsze(int m){
        vector<int> tablica;
        while(m!=1){
            tablica.push_back(tab[m]);
            m=m/tab[m];
        }
    delete [] tab;

return tablica;

}




//////////////////GLOWNY BLOK///////////////////////////////////////

int main(){
    RozkladLiczby odp = RozkladLiczby(10);
    cout<<"CHECK1"<<endl;

 vector<int> p = odp.CzynnikiPierwsze(5);

    //for(int i=0;i<=1;i++)
    //  cout<< p[i] <<endl;



return 0;

}

I would be grateful if u could help me.

Was it helpful?

Solution

Error 1

Your constructor performs a memory allocation like so:

int* tab = new int[n+1];

This will put the result in a local variable named tab, not the member field of the same name. Your constructor then does everything with this local variable and the member field is never initialized.

Therefore your CzynnikiPierwsze is performed with an uninitialized pointer and causes undefined behavior as soon as it accesses it. To compound your problems, you even try to free this never initialized pointer at the end of that function.

Error 2

Also, there is another error in your commented out code. After initialization your array would contain 0 0 2 3 2 5 2 7 2 3 2. The CzynnikiPierwsze function when called with 5 would first push_back tab[5] which is 5. Therefore, m immediately becomes 1 and the vector is returned. Note that only one push_back was performed.

You then try (in the code you commented out) to get p[0], which is ok and p[1], which does not exist and, again, causes undefined behaviour - or an exception if you are lucky.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top