0
typedef struct _lnode{
    struct _lnode *next;
    size_t row;
    size_t column;
    short data;
}lnode;

typedef struct _matrix{

    size_t width;
    size_t height;
    size_t k;

    int **data;

}matrix;

matrix* make_matrix(size_t width, size_t height, size_t k){

    matrix *m= malloc(sizeof(matrix));
    //matrix *m= malloc(sizeof(*matrix)); DOES NOT WORK
    if(m==NULL) return NULL;

    m->width = width;
    m->height = height;

    /*
    Since m->data is a int **, it points to int *, 
    so I have to allocate a number of int *-sized objects to store in it.
    */
    m->data = malloc(sizeof(int *)*height);
    if(m->data == NULL){
        free(m);
        return NULL;
    }

    for(size_t i=0; i < height; i++){
        m->data[i] = malloc(sizeof(int)*width);
        if(m->data[i] == NULL){
            for(size_t j = 0; j < i; j++) free(m->data[j]);
            free(m->data);
            free(m);
            return 0;
        }

        for(size_t j = 0; j < width; j++)
            m->data[i][j] = 0;
    }

    return m;
}


lnode* make_node(size_t row, size_t column, short data){
    lnode *newNode = malloc(sizeof(*newNode));

    if(newNode==NULL) return NULL;

    newNode->row = row;
    newNode->column = column;
    newNode->data = data;

    return newNode;
}

These two functions work fine. In the make_matrix function, I first tried this

 matrix *m= malloc(sizeof(*matrix)); 

instead of

 matrix *m= malloc(sizeof(matrix));

Then it only works for the first iteration in the for loop and falls into the if statement

if(m->data[i] == NULL){
                for(size_t j = 0; j < i; j++) free(m->data[j]);
                free(m->data);
                free(m);
                return 0;
            }

I know that sizeof(pointer) will return a size of the pointer. In make_node function I am doing lnode *newNode = malloc(sizeof(*newNode)); and it worked fine. I am trying to do the same thing in make_matrix function. It doesn't work this time...

1
  • 2
    It works fine (as it should) with ` matrix *m= malloc(sizeof(matrix));`? If so, what is the question?
    – MAK
    Mar 25, 2011 at 6:15

3 Answers 3

2

newNode is a variable, so sizeof *newnode returns the size of thing it wants to point to. The equivalent in make_matrix() would be sizeof *m, not sizeof *matrix.

1
  • 2
    I may just be tired, but I'm not even sure what sizeof(*matrix) means to the compiler. matrix is a typedef, so *matrix shouldn't be meaningful (the correct syntax would be (matrix *)).
    – geekosaur
    Mar 25, 2011 at 6:19
0

You say "I know that sizeof(pointer) will return a size of the pointer"

But I don't think you do.

You need to do:

matrix *m = malloc(sizeof(matrix));

Otherwise you get 4 bytes (or 8, if your machine is using 64bit pointers).

"It works" with what you're doing is not correct. The behavior is undefined. You just got lucky.

0

If I understand your question correctly you are saying that your code fails because m->data[i] is not null?

malloc will not initialize your variable, the contents of what you get is unpredictable. Use calloc to initialize to all bits zero, or even better if you have a standard conforming compiler use something like

*m = (matrix){ 0 };

to initialize all fields to 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.