A problem when creating a matris

T

Tony Johansson

Hello Experts!!

I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************
for (int i=0; i < c_rader; i++)
matris = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};
 
V

Victor Bazarov

Tony said:
I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;


Your class uses dynamic memory. It is at this point missing a proper
copy-constructor and a proper destructor. Please read about "the Rule
of Three" and correct your code accordingly.
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************

'matris' is a pointer with indeterminate value at this point. You're
trying to dereference it. That's the reason for the crash (krasch).
What you should do, probably, is

matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};


You probably shouldn't attempt dynamic memory management without some
better understanding of pointers and operations on them.

V
 
T

Tony Johansson

Hello!!

I know I must have a copy constructor and a destructor but I just cut out
that to make it easier for you to understand the code.
//Tony

Victor Bazarov said:
Tony said:
I have two small classes called Intvektor and Matris shown below and a
main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of
Intvektor in this way I will create a matris. For example an matris of x
rows and y columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class
Matris.
The problem is something with pointer. I assume that I must have a
pointer to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;


Your class uses dynamic memory. It is at this point missing a proper
copy-constructor and a proper destructor. Please read about "the Rule
of Three" and correct your code accordingly.
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************

'matris' is a pointer with indeterminate value at this point. You're
trying to dereference it. That's the reason for the crash (krasch).
What you should do, probably, is

matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};


You probably shouldn't attempt dynamic memory management without some
better understanding of pointers and operations on them.

V
 
L

Larry I Smith

Tony said:
Hello Experts!!

I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************
for (int i=0; i < c_rader; i++)
matris = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};


---- Matrix.h ----

// Matrix.h
#include <vector>
#include <iostream>

// a vector of int's
typedef std::vector< int > Intvektor;

// a vector of Intvektor's (i.e. a 2 dim matrix of int's)
typedef std::vector< Intvektor > Int2vektor;


// class Matrix derived from a vector of vectors of int.
// all of the features of 'vector' are available to Matrix.
class Matrix : public Int2vektor
{
public:
Matrix() : Int2vektor()
{
}

Matrix(int row, int col) : Int2vektor(row, Intvektor(col))
{
}

Matrix(const Matrix& m) : Int2vektor(m)
{
}

// dump my contents to 'os' for debugging
std::eek:stream& dump(std::eek:stream& os, const char * title = NULL)
{
if (title)
os << title << std::endl;

for (int row = 0; row < size(); row++)
{
os << "row: " << row << std::endl;
for (int col = 0; col < this->operator[](row).size(); col++)
os << " " << this->operator[](row)[col];
os << std::endl;
}

os << std::endl;

return os;
}
};

---- end of Matrix.h ----

---- MatrixTest.cpp ----

// Matrix.cpp
#include "Matrix.h"

int main()
{
Matrix a(2,2);

// dump the initial contents of 'a'
a.dump(std::cout, "'a' initial contents");

int v = 0;

// put some values into 'a'
for (int row = 0; row < a.size(); row++)
for (int col = 0; col < a[row].size(); col++)
a[row][col] = v++;

// dump the new contents of 'a'
a.dump(std::cout, "'a' with values added");

// make 'b' as a copy of 'a'
Matrix b(a);

// dump the contents of 'b'
b.dump(std::cout, "'b' as a copy of 'a'");

// erase 'a'
a.clear();

// demonstrate that the copy-constructor
// actually copied the data from 'a' to 'b'
a.dump(std::cout, "'a' after a.clear()");
b.dump(std::cout, "'b' after a.clear()");

// copy 'b' to 'a'
a = b;
a.dump(std::cout, "'a' after 'a = b'");

return 0;
}

---- end of MatrixTest.cpp ----

Regards,
Larry
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top