Help with dynamic memory

S

Silver

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 << "Constructor for class A called.";
fl = 1;
row1 = n;
column1 = n;

ppa = (float **)malloc(n * sizeof(float *));
for(int i = 0; i < n; i++)
ppa = (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[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 << "Constructor 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;
}
}

void multi(A &a, X &x) // array multiplication
{
cout<<"\n\nMatrix Multiplication\n";
for(int i = 0; i < a.row1; i++)
{
x.b = 0;
for (int k=0 ; k < x.row2; k++)
{
x.b += a.ppa[k] * x.x[k];
}
cout << setw(5) << x.b << 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;
}
 
G

Gianni Mariani

Silver said:
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;

What is "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 << "Constructor for class A called.";
fl = 1;
row1 = n;
column1 = n;

ppa = (float **)malloc(n * sizeof(float *));

use new. Or even better - use std::vector said:
for(int i = 0; i < n; i++)
ppa = (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.";
}


you can use ppa instead of fl ...
}

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[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;}


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.
friend void multi(A &a, X &x);
};

X::X(int n, int m)
{
cout << endl << "Constructor for class X called.";
x = (float *)malloc(m * sizeof(float));
b = (float *)malloc(n * sizeof(float));

use a std::vector.
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;
}
}

void multi(A &a, X &x) // array multiplication
{
cout<<"\n\nMatrix Multiplication\n";
for(int i = 0; i < a.row1; i++)
{
x.b = 0;
for (int k=0 ; k < x.row2; k++)
{
x.b += a.ppa[k] * x.x[k];
}
cout << setw(5) << x.b << 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);


setX initializes the "b" array but "multi" below uses the "x" array ....
X b(grammes, stiles);

multi(a,x);

return 0;
}

suggestions:

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

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
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top