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;
} 1 1962
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 << "Constructo r for class A called."; fl = 1; row1 = n; column1 = n;
ppa = (float **)malloc(n * sizeof(float *));
use new. Or even better - use std::vector<flo at>
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 << "Constructo r 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\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);
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Greg Baker |
last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to
post code and ask for help? I hope so.. :)
Here's my problem: I am trying problem 127 of the valladolid online
contests (http://online-judge.uva.es/p/v1/127.html). The program I wrote
seems to work fine, but it takes way too much memory to run. I am not that
good at programming C++, unfortunately, so I can't seem to find my memory
leak. As far as I can tell,...
|
by: Tommy Lang |
last post by:
I am trying to learn to use dynamic variables.
I have pasted the code below.
Is this the proper way of using dynamic variables?
Thanks,
Tommy
//------------------------------------------------------------
#include <iostream>
|
by: chris |
last post by:
Hi all,
I need to know, what is the difference between dynamic memory
allocation, and stack allocation ?
1. If I have a class named DestinationAddress, when should I use
dynamic memory allocation to create object of that class ?
2. If it says "dynamic memory allocation", is it mean the
following code :
DestinationAddress* dest = new DestinationAddress(); // code 1
|
by: Pasacco |
last post by:
dear
I want to ask help on this problem.
Array a is partitioned into a0 and a1 in main(). Then
a1 is partitioned into a2 and a3 in th_partition()
function. And I think this problem is something about parameter
passing.
If someone give me comment, it will be thankful.
thankyou very much
|
by: Stephen Gennard |
last post by:
Hello,
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example below...
--
| |
by: ashmangat |
last post by:
Hey I am new, but I don't have time to intruduce myself yet.
I am intro to C++
and this is a programme I have to write.
all the direction are here, It will be very nice of someone to figure
this out.
note: I only in intro C++ which is about to be finished by the next two
weeks.
The topic which I am on in my book is "POINTERS"
Text from the book:
|
by: vinod.bhavnani |
last post by:
Hello all,
I need desperate help
Here is the problem:
My problem today is with multidimensional arrays.
Lets say i have an array A this is a 4 dimensional
static array.
|
by: deppy_3 |
last post by:
Hi.I am started learning Programm language C before some time.I am
trying to make a programm about a very simple "sell shop".This programm
hasn't got any compile problem but when i run it i face some other
ploblems which i can not correct.I would appreciated if someone take a
look at my programm so as to help me.I tried many times to find out
where my mistakes are but i didn't manage something.
I have made some comments due to the programm...
|
by: Ken |
last post by:
In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.
Please comment... thanks.
|
by: vivek |
last post by:
i have some doubts on dynamic memory allocation and stacks and heaps
where is the dynamic memory allocation used?
in function calls there are some counters like "i" in the below
function. Is this stored in stack. If yes whether it will be deleted
on exiting from the function.
is dynamic memory allocation needed for this purpose
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |