problems: (1)dynamic matrix & (2) with global matrix

Hello,

(1) From the code 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
#include <stdlib.h>
#include <stdio.h> 

int **a; // global matrix.

int main(void){
    int dim = 100;
    
    int size_x = dim; /* X_dimension */
    int size_y = dim; /* Y_dimension */
    
    /*  allocate storage for an array of pointers */
    a = malloc(size_x * sizeof(int *));
    
    /* for each pointer, allocate storage for an array of ints */
    for (int i=0; i < size_x; i++) {
        a[i] = malloc(size_y * sizeof(int));
    }
    
    for(int i =0; i < size_x; i++)
            for(int j=0; j < size_y;j++)
    
    /* now for each pointer, free its array of ints */
    for (int i = 0; i < size_y; i++) {
        free(a[i]);
    }
    
    /* now free the array of pointers */
    free(a);
    
    return 0;
}


i receive the following compiler errors using either gcc or g++:
13: error: invalid conversion from void* to int**
17: error: invalid conversion from void* to int**


How to fix it,please ?

(2) i have the code

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>
#include <vector.h> 

int main(void){
    /*
        ...code which determines the dim variable...
    */
    vector<vector<string> >mat(dim, vector<string>(dim)); // matrix mat
    
    return 0;
}


i want the matrix mat to be a global matrix. What i am thinking is to use the (1) code for that. However, i would like to ask how i could do this with vector?

Thank you.
Last edited on
http://www.cplusplus.com/forum/articles/7459/

I don't know why your original code didn't work; maybe it's because of C++ compilers.
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
#include <stdlib.h>
#include <stdio.h>

int **a; // global matrix.

int main(void){
    int dim = 100;

    int size_x = dim; /* X_dimension */
    int size_y = dim; /* Y_dimension */

    /*  allocate storage for an array of pointers */
	a = new int*[size_x];

    /* for each pointer, allocate storage for an array of ints */
    for (int i=0; i < size_x; i++) {
		a[i] = new int[size_y];
    }

    for(int i =0; i < size_x; i++)
            for(int j=0; j < size_y; j++)
				printf("back to C\n");

    /* now for each pointer, free its array of ints */
    for (int i = 0; i < size_y; i++) {
        delete[] a[i];
    }

    /* now free the array of pointers */
    delete[] a;

    return 0;
}
Last edited on
Thanks Catfish, it works now perfect.

The 1st question is OK.

For the 2nd question, i would like also to ask, what's it better to use vector or malloc-free and why ?
You should never use malloc-free is C++. Instead use new-delete.

And always use a std::vector<> rather than allocating the memory yourself. It is much safe because you don't need to remember to free the memory yourself.
How can i have a global vector if i dont know from the beggining the size of the matrix ? For example in this code:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>
#include <vector.h> 

int main(void){
    /*
        ...code which determines the size_x, size_y variables...
    */
    vector<vector<string> >mat(size_x, vector<string>(size_y)); // matrix mat
    
    return 0;
}


Last edited on
The vector class has a resize() member function that will change the vector's size to how much is asked.
I have the code below, where the matrix mat is global.

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
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int> >mat;

void func(void){
     
     for(int i=0; i<5; i++)
            for(int j=0; j<5; j++)
                    mat[i][j] = 0;
          
}

int main(void){

    int size_x = 5; // X dimension
    int size_y = 5; // Y dimension

    vector<vector<int> >mat(size_x, vector<int>(size_y)); // matrix mat
    
    for(int i=0; i<size_x; i++)
            for(int j=0; j<size_y; j++)
                    mat[i][j] = i;
    
    
    for(int i=0; i<size_x; i++){
            cout << "\n";
            for(int j=0; j<size_y; j++){
                    cout << mat[i][j] << " ";
            }
    }
    
    func(); 
    
    cout << "\n\n";
    for(int i=0; i<size_x; i++){
            cout << "\n";
            for(int j=0; j<size_y; j++){
                    cout << mat[i][j] << " ";
            }
    }
    
    cout << "\n\n";
    system("PAUSE");
    return 0;
}


the output i receive is:
0 0 0 0 0 
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
Segmentation fault


The problem is caused probably from the function func().

What am i doing wrong ?
Last edited on
Topic archived. No new replies allowed.