Constructor issue

So I created a constructor to create a Matrix, I can create one Matrix just fine, but as soon as I create a second I get a segmentation fault error.

Here's the relevant line in my header file
 
   Matrix(uint rows, uint cols);


Here's my constructor in the .cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Matrix::Matrix(uint rows, uint cols){
  array = new double*[rows];
  cout<<"test: "<< rows;
    for(uint i = 0; i < rows; i++){
      array[i] = new double[cols];
    }

    for(uint i = 0; i < rows; i++){
      for(uint j = 0; j < cols; j++){
	array[i][j] = 0.0;
      }
    }
    
  }


Any ideas What I'm doing wrong?
In line 2 of your .cpp did you want to create a pointer array on purpose? Because you don't have any references to memory or pointers with the rest of your code.
Ya, so the point of the project is to learn about dynamic memory allocation so we're not allowed to use the array or vector class. In my header file I also have public variables:
[code]
double **array;
uint rows;
uint cols;
I made a void function that prints the matrix, is very similar to your constructor, and it works. The error might be somewhere else. The only difference is that instead of uint, I used unsigned int. Check your uint object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void matrix(unsigned int numRow, unsigned int numCol)
{
	int **c;
	c = new int*[numRow];


	for(unsigned int row = 0; row<numRow; row++)
	{
		c[row]= new int[numCol];
	
	}


	for(unsigned int row = 0; row<numRow; row++)
	{
		for(unsigned int col = 0; col<numCol; col++)
		{
			c[row][col] = 0;
			cout << c[row][col] << ' ';
		}
	
		cout << endl;

	}
}
Last edited on
I declared
 
typedef unsigned int uint;

In my header, so that can't be it.
Ya that's just like mine, I don't know why mine is acting up.
Also, as soon as I add the cout that you have in yours, it gives me the same segmentation dump error.
Would the error possibly lie with the compiler you're using? Like I haven't heard the greatest things about Turbo C++ and stuff like that. I won't be able to help you out if the issue originates there but at least we can get some info to work with.
It's just a basic g++ compiler on linux. I really doubt that would be the issue, I'm very inexperienced in C++ so I almost guarantee it's an error on my part.
How is the constructor called?
Post both your .cpp and .h? If there's a definitive error there, we'll be able to tell right away. Also what's the exact error message you're getting from the compiler? Might point us in the right direction as well.
Matrix a(2,2);
Here's the main testing class:
1
2
3
4
5
6
7
8
#include"Matrix.h"
#include<cstdlib>
#include<iostream>


int main(){
  Matrix a(2,2);
}
And here's the terminal command:

g++ -Wall -std=c++11 -g -O0 -pedantic-errors ./Matrix.cpp ./lab04.cpp -o test03

(We had to use this, the instructor told us to. I have no idea what most of it's doing)
Last edited on
Hey @YFGHNG, I sent you some more information about the error code I'm getting
I still don't see any errors. I compiled the program below using:

g++ -Wall -std=c++11 -g -O0 -pedantic-errors matrix.cpp -o matrix.exe

or

g++ -Wall -std=c++11 -g -O0 -pedantic-errors ./matrix.cpp -o ./matrix.exe

The './' doesn't matter since it's in the same folder as that's open in the terminal. The './' just denotes that you can navigate to different folders and also compile the exe to different folders.

matrix.cpp file is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

void matrix(unsigned int numRow, unsigned int numCol)
{
	int **c;

	c = new int*[numRow];


	for(unsigned int row = 0; row<numRow; row++)
	{
		c[row]= new int[numCol];
	
	}


	for(unsigned int row = 0; row<numRow; row++)
	{
		for(unsigned int col = 0; col<numCol; col++)
		{
			c[row][col] = 0;
			cout << c[row][col] << ' ';
		}
	
		cout << endl;

	}
}


int main()
{
	matrix(10, 20);

	return 0;

}


You can look up compiler options here:

https://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_3.html
Last edited on
So I ran GDB and found that the segmentation fault is resulting from using free(). Why would that be??

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff727b56c in free () from /lib64/libc.so.6
Last edited on
Is free() your destructor? You should post the code. The destructor for a class is called every time an object goes out of scope or when delete[] is applied to a pointer that points to an object class. When you do dynamic allocation, always implement your own destructor, otherwise you have memory leak.

Look at the bottom of the page for destructor tutorial:
http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm

You have to use your destructor to free the memory allocated in your dynamic array before deleting the pointer. You need to do this every time you have a dynamically allocated variable in your class. The default destructor will just delete your pointer and not the actual content the pointer points to. If this happens, all access to your data is lost, meaning it cannot be accessed or deleted. Hence we call this a memory leak.
Last edited on
We're not seeing enough code to be of help yet, but I noticed you stated two members, cols and rows, in the matrix class, but you're also using those names as parameters in the constructor.

Consider separating those to avoid confusion if that's the case.
Topic archived. No new replies allowed.