Help with dynamic memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Silver

    Help with dynamic memory

    Hi everyone,

    my program compiles and executes, but I get an error during run-time. I
    guess it has something to do with memory allocation (which I don't seem to
    fully control yet).. Here's the code:

    #include <iostream>
    #include <malloc.h>
    #include <iomanip>
    using namespace std;

    class X;
    class A
    {
    private:
    float** ppa;
    int fl;
    int row1;
    int column1;

    public:
    A(int n, int m); // Constructor
    A(); // Default constructor
    ~A(); // Destructor
    void setA(int n, int m);
    void set_fl() {fl = 0;}

    friend void multi(A &a, X &x);
    };

    A::A(int n, int m)
    {
    cout << endl << "Constructo r for class A called.";
    fl = 1;
    row1 = n;
    column1 = n;

    ppa = (float **)malloc(n * sizeof(float *));
    for(int i = 0; i < n; i++)
    ppa[i] = (float *)malloc(m * sizeof(float));
    }

    A::A()
    {
    cout << endl << "Default constructor for class A called.";
    fl = 0;
    }

    A::~A()
    {
    if(fl)
    {
    free((void **)ppa);
    cout << endl << "Destructor for class A called.";
    }
    }

    void A::setA(int n, int m)
    {
    for (int i = 0; i < n; i++) // initialization of a[n][m]
    { // this is for a
    for (int j = 0; j < m; j++)
    {
    cout << "\nDWSE TO STOIXEIO " << "a[" << i << "][" << j << "]";
    cin >> ppa[i][j];
    }
    }
    }

    class X
    {
    private:
    float* x;
    float* b;
    int row2;
    int column2;
    int fl;

    public:
    X(int n, int m);
    ~X();
    void setX(int n, int m);
    void set_fl() {fl = 0;}

    friend void multi(A &a, X &x);
    };

    X::X(int n, int m)
    {
    cout << endl << "Constructo r for class X called.";
    x = (float *)malloc(m * sizeof(float));
    b = (float *)malloc(n * sizeof(float));
    row2 = n;
    column2 = 1;
    }

    X::~X()
    {
    if (fl=1)
    {
    free((void *)x);
    free((void *)b);
    }
    cout << endl << "Destructor for class X called.";
    }

    void X::setX(int n, int m)
    {
    for (int i = 0; i < m; i++) // Initialization of b[m]
    {
    cout << "\nDWSE TO STOIXEIO " << "b[" << i << "]";
    cin >> b[i];
    }
    }

    void multi(A &a, X &x) // array multiplication
    {
    cout<<"\n\nMatr ix Multiplication\ n";
    for(int i = 0; i < a.row1; i++)
    {
    x.b[i] = 0;
    for (int k=0 ; k < x.row2; k++)
    {
    x.b[i] += a.ppa[i][k] * x.x[k];
    }
    cout << setw(5) << x.b[i] << endl;
    }
    a.set_fl();
    x.set_fl();
    }

    int main()
    {
    int grammes = 0 ; // rows
    int stiles = 0 ; // colums
    int i = 0; // index for loop
    int j = 0; // index for loop

    cout << "PROGRAM FOR MATRIX MULTIPLICATION\ n";
    cout << "---------------------------------\n\n";
    cout << "\nROWS? ";
    cin >> grammes;
    cin.ignore(); // to get rid of newline

    cout << "\nCOLUMNS? ";
    cin >> stiles;
    cin.ignore();

    A a(grammes, stiles);
    a.setA(grammes, stiles);

    X x(grammes, stiles);
    x.setX(grammes, stiles);

    X b(grammes, stiles);

    multi(a,x);

    return 0;
    }


  • Gianni Mariani

    #2
    Re: Help with dynamic memory

    Silver wrote:[color=blue]
    > Hi everyone,
    >
    > my program compiles and executes, but I get an error during run-time. I
    > guess it has something to do with memory allocation (which I don't seem to
    > fully control yet).. Here's the code:
    >
    > #include <iostream>
    > #include <malloc.h>
    > #include <iomanip>
    > using namespace std;
    >
    > class X;
    > class A
    > {
    > private:
    > float** ppa;
    > int fl;[/color]

    What is "fl" ?
    [color=blue]
    > int row1;
    > int column1;
    >
    > public:
    > A(int n, int m); // Constructor
    > A(); // Default constructor
    > ~A(); // Destructor
    > void setA(int n, int m);
    > void set_fl() {fl = 0;}
    >
    > friend void multi(A &a, X &x);
    > };
    >
    > A::A(int n, int m)
    > {
    > cout << endl << "Constructo r for class A called.";
    > fl = 1;
    > row1 = n;
    > column1 = n;
    >
    > ppa = (float **)malloc(n * sizeof(float *));[/color]

    use new. Or even better - use std::vector<flo at>
    [color=blue]
    > for(int i = 0; i < n; i++)
    > ppa[i] = (float *)malloc(m * sizeof(float));
    > }
    >
    > A::A()
    > {
    > cout << endl << "Default constructor for class A called.";
    > fl = 0;
    > }
    >
    > A::~A()
    > {
    > if(fl)
    > {
    > free((void **)ppa);
    > cout << endl << "Destructor for class A called.";
    > }[/color]

    you can use ppa instead of fl ...
    [color=blue]
    > }
    >
    > void A::setA(int n, int m)
    > {
    > for (int i = 0; i < n; i++) // initialization of a[n][m]
    > { // this is for a
    > for (int j = 0; j < m; j++)
    > {
    > cout << "\nDWSE TO STOIXEIO " << "a[" << i << "][" << j << "]";
    > cin >> ppa[i][j];
    > }
    > }
    > }
    >
    > class X
    > {
    > private:
    > float* x;
    > float* b;
    > int row2;
    > int column2;
    > int fl;
    >
    > public:
    > X(int n, int m);
    > ~X();
    > void setX(int n, int m);
    > void set_fl() {fl = 0;}[/color]

    Look up "the c++ rule of 3".

    See references to th "Rule of 3": if your class has either a destructor
    or a copy constructor or an assignment operator, it probably needs all 3
    of them.
    [color=blue]
    >
    > friend void multi(A &a, X &x);
    > };
    >
    > X::X(int n, int m)
    > {
    > cout << endl << "Constructo r for class X called.";
    > x = (float *)malloc(m * sizeof(float));
    > b = (float *)malloc(n * sizeof(float));[/color]

    use a std::vector.
    [color=blue]
    > row2 = n;
    > column2 = 1;
    > }
    >
    > X::~X()
    > {
    > if (fl=1)
    > {
    > free((void *)x);
    > free((void *)b);
    > }
    > cout << endl << "Destructor for class X called.";
    > }
    >
    > void X::setX(int n, int m)
    > {
    > for (int i = 0; i < m; i++) // Initialization of b[m]
    > {
    > cout << "\nDWSE TO STOIXEIO " << "b[" << i << "]";
    > cin >> b[i];
    > }
    > }
    >
    > void multi(A &a, X &x) // array multiplication
    > {
    > cout<<"\n\nMatr ix Multiplication\ n";
    > for(int i = 0; i < a.row1; i++)
    > {
    > x.b[i] = 0;
    > for (int k=0 ; k < x.row2; k++)
    > {
    > x.b[i] += a.ppa[i][k] * x.x[k];
    > }
    > cout << setw(5) << x.b[i] << endl;
    > }
    > a.set_fl();
    > x.set_fl();
    > }
    >
    > int main()
    > {
    > int grammes = 0 ; // rows
    > int stiles = 0 ; // colums
    > int i = 0; // index for loop
    > int j = 0; // index for loop
    >
    > cout << "PROGRAM FOR MATRIX MULTIPLICATION\ n";
    > cout << "---------------------------------\n\n";
    > cout << "\nROWS? ";
    > cin >> grammes;
    > cin.ignore(); // to get rid of newline
    >
    > cout << "\nCOLUMNS? ";
    > cin >> stiles;
    > cin.ignore();
    >
    > A a(grammes, stiles);
    > a.setA(grammes, stiles);
    >
    > X x(grammes, stiles);
    > x.setX(grammes, stiles);[/color]

    setX initializes the "b" array but "multi" below uses the "x" array ....
    [color=blue]
    >
    > X b(grammes, stiles);
    >
    > multi(a,x);
    >
    > return 0;
    > }[/color]

    suggestions:

    a. Your memory allocation problem goes away when you use std::vector.
    b. look up some other standard matrix library.

    Comment

    Working...