By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Help with dynamic memory

P: n/a
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[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 << "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[i];
}
}

void multi(A &a, X &x) // array multiplication
{
cout<<"\n\nMatrix 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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Silver wrote:
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<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.";
}
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[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;}
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[i];
}
}

void multi(A &a, X &x) // array multiplication
{
cout<<"\n\nMatrix 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);
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.

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.