2

I'm having a rough time with pointers, can someone help me a little bit?

I'm trying to initialize a double (double) pointer inside a struct array, but I'm somehow doing it wrong.. Example:

struct MyStruct
{
    double **matrix;
};

double **CalculateMatrix(...)
{
    double **matrix = (double**)malloc(ROWS * sizeof(double*));
    for (int i = 0; i < ROWS; i++)
        matrix[i] = (double*)malloc(COLS * sizeof(double));
    // + assign some values
    return matrix;
}

void Initialize(struct MyStruct *structs, int size)
{
    for (int i = 0; i < size; i++)
        // Here structs[i].matrix holds the correct matrix values
        structs[i].matrix = CalculateMatrix(...);
}

int main()
{
    struct MyStruct *structs = (struct MyStruct*)malloc(SIZE * sizeof(struct MyStruct));
    Initialize(structs, SIZE);
    // but once the function returns, matrix values are completely different
}

Sorry if it's duplicated, I couldn't find anything

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    did you forgot to pass `size` to `Initialize(structs);`? It should give compilation error or warning. – kiran Biradar Nov 21 '18 at 09:08
  • 1
    The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you *don't* show. Try to use a memory debugger such a [Valgrind](http://valgrind.org/) (or similar) to find out if you're having some problems with out-of-bounds access or similar. – Some programmer dude Nov 21 '18 at 09:09
  • 1
    Oh and in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Some programmer dude Nov 21 '18 at 09:10
  • Yeah, forgot to add the `size` variable, it's just an example anyways. The only real difference with the real code it's that the `structs` defined inside main it's a static global variable, does that change anything? – Borja Sanchidrián Monge Nov 21 '18 at 09:11
  • And please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly try to create an [mcve] of the failing code to show us. – Some programmer dude Nov 21 '18 at 09:11
  • Or you [should cast](https://stackoverflow.com/a/14879184/1312382)? Admitted, a minority oppinion, but too large just to be ignored. My recommendation now always is: read both and make up *your own mind*... – Aconcagua Nov 21 '18 at 09:56

2 Answers2

1

You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.

Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.

Here is a minimal example to get you started:

#include <stdlib.h>
#include <stdio.h>

struct MyStruct
{
    double **matrix;
};

double **allocate_matrix(int rows, int cols)
{
    double **matrix = malloc(rows * sizeof(double*));
    for (int i = 0; i < rows; i++)
        matrix[i] = malloc(cols * sizeof(double));
    return matrix;
}

void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
    for (int i = 0; i < size; i++)
        structs[i].matrix = allocate_matrix(rows, cols);
}

void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
    for (int i = 0; i < size; i++)
        for(int j = 0; j < rows; j++)
            for(int z = 0; z < cols; z++)
                structs[i].matrix[j][z] = -1.2;
}

void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
    for (int i = 0; i < size; i++)
        for(int j = 0; j < rows; j++)
            for(int z = 0; z < cols; z++)
                printf("%f\n", structs[i].matrix[j][z]);
}

void free_matrices(struct MyStruct *structs, int size, int rows) {
    for (int i = 0; i < size; i++) {
        for(int j = 0; j < rows; j++) {
            free(structs[i].matrix[j]);
        }
        free(structs[i].matrix);
    }
}


int main()
{
    int rows = 3, cols = 4, size = 2;
    struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
    structs[0].matrix = NULL;
    structs[1].matrix = NULL;
    if(structs[0].matrix == NULL)
        printf("null\n");
    allocate_matrices(structs, size, rows, cols);
    if(structs[0].matrix == NULL)
        printf("null\n");
    fill_matrices(structs, size, rows, cols);
    print_matrices(structs, size, rows, cols);
    free_matrices(structs, size, rows);
    free(structs);
}

Output:

null
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000

Inspired from my 2D dynamic array (C).

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

Extending gsamaras' answer:

'You do not need global variables here' is truth. Still, having constants ROWS and COLS can be meaningful as well. It depends on the use case!

gsamaras' approach allowes you to define individual sizes for each matrix. If you want to enforce same size for all matrices created, it is less error prone not to have parameters for but constants instead.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59