1

I've trying to allocate an unsigned char** using calloc:

newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char));

for (j=0 ; j < width; j++)
{
    if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){
        printf("Memory allocation error. Exit program\n");
        exit(1);
    }
}

for (i=0;i<width;i++)
{
    for(j=0;j<width;j++)
    {
        newmatriz[i][j] = 0;
    }
}

But I get segmentation fault when I'm trying to acces the pos [i][j]

Is problem be related to the use int as iterator?

user3055867
  • 17
  • 1
  • 8
  • 3
    should be `newmatriz=calloc(width, sizeof(unsigned char*))` – BLUEPIXY Jun 03 '15 at 16:33
  • Do **not** cast `void *` as returned by `calloc()`&friends. Also: do not use `sizeof(char)` (signed/unsigned the same). That will never be different than 1, as that's what is is explicitly defined to yield by the standard. – too honest for this site Jun 03 '15 at 16:33
  • 1
    The code is missing an asterisk (see BLUEPIXY's comment). Voting to close as a typo. – Sergey Kalinichenko Jun 03 '15 at 16:38
  • I removed the cast from calloc, still getting the same error in fact, I get this warning when compiling: filtrodefinitivo.c:298:29: warning: assignment makes pointer from integer without a cast [enabled by default] if (newmatriz[j]=calloc(width, sizeof(unsigned char)) == NULL){ – user3055867 Jun 03 '15 at 16:40
  • 1
    @user3055867, that's a good reason not to explicitly cast the return value of `malloc` family of functions. You were hiding a problem by using explicit cast. – R Sahu Jun 03 '15 at 16:46
  • It's already included. – user3055867 Jun 03 '15 at 16:49
  • ... and if it's not the typo that causes the error it might very well be the missing include. Btw.: Have you edited your question because you haven't had the typo in the original code? The problem is, that the answers now don't make sense anymore – Ingo Leonhardt Jun 03 '15 at 16:50
  • `if (newmatriz[j]=calloc(width, sizeof(unsigned char)) == NULL){` shoud be `if ((newmatriz[j]=calloc(width, sizeof(unsigned char))) == NULL){ ` – BLUEPIXY Jun 03 '15 at 16:55
  • I did rollback on the code, sorry. And thanks to all, was a brackets problem! Neverminded – user3055867 Jun 03 '15 at 16:59

3 Answers3

2

There is a typo in this statement. Instead of sizeof( unsigned char ) you have to use sizeof( unsigned char * )

newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char *));
                                                            ^^^^^^

Also this if statement is incorrect

if ( newmatriz[j] = calloc(witdh, sizeof(unsigned char) ) == NULL){

In this statement newmatriz[j] is set either to 1 or 0 depending on whether the memory allocation was successfull or not.

I think you mean

if ( ( newmatriz[j] = calloc(witdh, sizeof(unsigned char) ) ) == NULL){

And these loops

for (i=0;i<width;i++)
{
    for(j=0;j<width;j++)
    {
        newmatriz[i][j] = 0;
    }
}

do not make sense because calloc already initialized the allocated memory by zeroes.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The answer is simple newmatriz is an array (aka pointer) to unsigned char* you are allocation unsigned char simply change the top line to allocate the correctly sized array, in this case you want an array of byte size width*sizeof(unsigned char*) not width*sizeof(unsigned char) as you currently have it.

newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char*));
for (j=0 ; j < width; j++){
    if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){
       printf("Memory allocation error. Exit program\n");
        exit(1);
    }
}
ceorron
  • 1,230
  • 1
  • 17
  • 28
0

In the second calloc() there are some brackets missing:

if (newmatriz[j] = calloc(witdh, sizeof(unsigned char)) == NULL){

should be

if ((newmatriz[j] = calloc(witdh, sizeof(unsigned char))) == NULL){

Without that, calloc()s result is compared with NULL and the result of the comparison instead of the pointer is stored in newmatriz[j]

Ingo Leonhardt
  • 9,435
  • 2
  • 24
  • 33