By sharp_c-tudent


2016-03-15 18:28:48 8 Comments

So my program was always returning 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 overlokking 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, wich I don't find very helpfull.

    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 on 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

3 comments

@sharp_c-tudent 2016-03-15 21:54:24

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

@chqrlie 2016-03-15 18:53:13

If the debugger shows you are triggering a segmentation fault on this line:

c->item1 = (int*) malloc((sizeof (int))*item1_);

It can mean two things:

  • c is a bad pointer, possibly NULL, but the previous statement c->typo = -1; should have failed too.

  • The arena is possibly corrupted and the problem is in the code executed before getting there.

@akinfermo 2016-03-15 18:47:42

I think your issue has to do with the way you're using malloc. Malloc allocates a block of bytes of memory, returning a pointer to the beginning of the block. You should write:

Capsula * c = (Capsula *) malloc (sizeof (struct capsula));

Actually, unless c is a pointer to a structure, it's illegal to write c->tipo = -1;. For example, the -> operator in c->tipo is a shortcut for *(c).tipo.

@Kaz 2016-03-15 18:49:27

If the upper-cased Capsula is typedef struct capsula *Capsula, there isn't anything wrong.

@chqrlie 2016-03-15 18:49:57

The OP is most likely hiding pointers behind typedefs: typedef struct capsula * Capsula;. This is considered bad style and your misinterpretation is the reason why it is counterproductive.

@sharp_c-tudent 2016-03-15 18:50:07

What you say is correct however my struct is capsula and Capsula is a pointer to it as I said on my question. But thanks for your answer anyway :)

@Myst 2016-03-15 18:56:12

@Kaz, I don't think we should down-vote for mistakes in code that is used to point at a problem. If the answer is correct, that code can be rectified instead. Finding the issue is the real challenge and I doubt we should be requiring people to test the code they use in the answers (I'm not talking about an answer with a long "this code will work for you" code block).

@sharp_c-tudent 2016-03-15 21:58:50

@Myst, Kaz didn't say his code was wrong, Oladodja just misinterpretated like chqrlie and kaz said. And actually on Oladodja's code there is nothing wrong.

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] Difference between malloc and calloc?

  • 2009-10-08 15:04:33
  • user105033
  • 538787 View
  • 791 Score
  • 14 Answer
  • Tags:   c malloc calloc

17 Answered Questions

[SOLVED] With arrays, why is it the case that a[5] == 5[a]?

47 Answered Questions

[SOLVED] How can I check for an empty/undefined/null string in JavaScript?

62 Answered Questions

[SOLVED] How to avoid null checking in Java?

30 Answered Questions

[SOLVED] How can I determine if a variable is 'undefined' or 'null'?

29 Answered Questions

[SOLVED] Do I cast the result of malloc?

  • 2009-03-03 10:13:02
  • Patrick McDonald
  • 244708 View
  • 2451 Score
  • 29 Answer
  • Tags:   c malloc casting

41 Answered Questions

6 Answered Questions

[SOLVED] Referring to the null object in Python

  • 2010-07-20 11:53:41
  • Lizard
  • 1700341 View
  • 1225 Score
  • 6 Answer
  • Tags:   python null

13 Answered Questions

[SOLVED] Altering a column: null to not null

7 Answered Questions

[SOLVED] Is null check needed before calling instanceof?

Sponsored Content