2

I have a problem when doing a malloc inside this code,

  /*function starts*/

   if(NULL==(partial_results=(bignum_t**)malloc(sizeof(bignum_t*)*long_op2))){
        return NULL;
    }       

    /*to initialize all the members of the array*/
    for(i=0;i<long_op2;i++){
        (*(partial_results+i))=NULL;
    }


    for(i=long_op2-1;i>=0;i--){
        digit2=op2->digits[i]-48;
        count++;
        carry=0;

        if(count==1){
            count2=0;
        }else{
            count2=count-1;
        }

        /*the next malloc is the one that fails*/
        if(NULL==(*(partial_results+(count-1))=(bignum_t*)malloc(sizeof(bignum_t)))){
            return NULL;
        }   

        /*after this the codes continues, but everything from here is ok an it isn't causing any problem*/

The thing here is, I'm trying to create an array of long_op2 elements (which is 9), so in the first malloc I create a 9 bignum_t pointers array. Then, inside the for, I try to create a bignum_t structure for each member of the array. When long_op2 is less or equal to 6 I have no problem, but when it's 7 or more, the first malloc, (the one that's suppose to create the pointers) doesn't work. I'm getting the error,

tp3: malloc.c:2372: sysmalloc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed. Aborted (core dumped)

The thing is, I'm not trying to write more than the quantity of the array created, because count gets to 8 max if long_op2 is 9, so that's ok. Another thing that's quite weird, is that when I run the program using Valgrind, it does work!

PD: It's my first question in here, so if I made any mistakes I apologize.

PD2: Here's how the program is working.

980581618*215129902

long_op1 & long_op2     9   9
for with: i, count-1    8   0
doing malloc malloc done 
for with: i, count-1    7   1
doing malloc malloc done 
for with: i, count-1    6   2
doing malloc malloc done 
for with: i, count-1    5   3
doing malloc malloc done 
for with: i, count-1    4   4
doing malloc malloc done 
for with: i, count-1    3   5
doing malloc malloc done 
for with: i, count-1    2   6
doing malloc malloc done 
for with: i, count-1    1   7
doing malloc malloc done 
for with: i, count-1    0   8
doing malloc 
tp3: malloc.c:2372: sysmalloc: Assertion ...
ad absurdum
  • 19,498
  • 5
  • 37
  • 60

1 Answers1

0

For this line:

    /*the next malloc is the one that fails*/
    if(NULL==(*(partial_results+(count-1))=(bignum_t*)malloc(sizeof(bignum_t)))){
        return NULL;
    }  

In any case:

count needs to be 1 <= count <= long_op2 . If it's outside that range, then you'll get undefined results. I can't see the initialization of count, but you should print the value of count out out before each malloc.

This is just a guess, but I suspect you really meant this:

    partial_results[i]=(bignum_t*)malloc(sizeof(bignum_t));
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Sorry I didn't comment on that count, it starts at value 1 (so the first position count-1=0) and it finishes at value 9 ( so the last position will be 8). I did print it out to know the value. And no, if I use [i], i starts in 8, and first I want to write the 0 position, that's why I use count-1. – Octavio Iza Nov 20 '16 at 17:36
  • So instead of `count`, use `long_op2 - 1 - i` as the index value. – selbie Nov 20 '16 at 18:13