cpointersnullmalloc

Why is malloc returning NULL here?


My program was always returns a segmentation fault, but I couldn't understand why so tried to debug with GDB and it showed me this:

(gdb) backtrace
#0  0x001a98ef in _int_malloc (av=0x2d8440, bytes=8) at malloc.c:3835
#1  0x001abedc in __GI___libc_malloc (bytes=8) at malloc.c:2924
#2  0x0804cd6a in init_capsula (item1_=2, item2_=2)
    at src/modulos/modulos_auxiliares/capsula/capsula.c:25
#3  0x0804d366 in total_dados_produto (f=0x8055838, filial=0x0, mes=6, 
    cod=0xbffff23c "AF1184") at src/modulos/faturacao/faturacao.c:208
#4  0x0804b237 in queries (q=3, c1=0x0, c2=0x0, f=0x8055838, v=0x0) at src/interface.c:815
#5  0x0804b6f4 in menu (c1=0x8055008, c2=0x8055420, f=0x8055838, v=0x0) at src/interface.c:976
#6  0x080487ad in main () at src/interface.c:1037

I then identified the source of the problem coming from frame 2 so decided to check that out and got the following output:

(gdb) frame 2
#2  0x0804cd6a in init_capsula (item1_=2, item2_=2)
    at src/modulos/modulos_auxiliares/capsula/capsula.c:25
25          c->item1 = (int*) malloc((sizeof (int))*item1_);

It tells me malloc is returning a NULL, however I can't see the problem with this line, everything is proprely initialized as I confirmed with my next action:

(gdb) print ((sizeof (int))*item1_)
$1 = 8

Why can't malloc allocate a such tiny amount of space? Am I overlooking something really stupid here?

I will put the function init_capsula here (the one where that malloc is) for you guys to see:

Capsula init_capsula(int item1_, int item2_){
     Capsula c = (Capsula) malloc (sizeof (struct capsula));

     c->tipo  = -1;

     if (item1_ > 0)
         c->item1 = (int*) malloc((sizeof (int))*item1_); /*Problematic line*/
     else c->item1 = NULL;

     if (item2_ > 0)
         c->item2 = (float*) malloc((sizeof (float))*item2_);
     else c->item2 = NULL;

     c->q1 = 0;
     c->q2 = 0;

     return c;
}

Capsula is a pointer to a struct defined like this:

struct capsula{
    int tipo;

    int     q1;
    int *item1;

    int       q2;
    float *item2;
 };

EDIT:

if I try to run with valgrind using the following command:

valgrind --tool=memcheck --leak-check=full make run

It outputs this, which I don't find very helpful.

    make: *** [run] Segmentation fault (core dumped)
    ==5848== 
    ==5848== HEAP SUMMARY:
    ==5848==     in use at exit: 62,771 bytes in 1,819 blocks
    ==5848==   total heap usage: 6,060 allocs, 4,241 frees, 580,609 bytes allocated
    ==5848== 
    ==5848== LEAK SUMMARY:
    ==5848==    definitely lost: 0 bytes in 0 blocks
    ==5848==    indirectly lost: 0 bytes in 0 blocks
    ==5848==      possibly lost: 0 bytes in 0 blocks
    ==5848==    still reachable: 62,771 bytes in 1,819 blocks
    ==5848==         suppressed: 0 bytes in 0 blocks
    ==5848== Reachable blocks (those to which a pointer was found) are not shown.
    ==5848== To see them, rerun with: --leak-check=full --show-reachable=yes
    ==5848== 
    ==5848== For counts of detected and suppressed errors, rerun with: -v
    ==5848== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

EDIT2:

I finally understood the problem by using valgrind correctly as I was using it on make when I should be using it on the program itself (as indicated in the comments). The problem was on a very different place, on a place I forgot to write a malloc, thanks for everyone that helped, now I finally understand how I should use valgrind


Solution

  • I am answering to my own question because, as I said on my last edit, I found my answer with help from the comments.

    I finally understood the problem by using valgrind correctly as I was using it on make when I should be using it on the program itself (as indicated on the comments). The problem was on a very different place, on a place I forgot to write a malloc so there is no need to get into details about that, thanks for everyone that helped, now I finally understand how I should use valgrind