2

I am trying to get the binary representation of a big integer in GMP. I am storing 1's and 0's in an array called expBinary. I use malloc to allocate a memory of size of "int", then use realloc to increase this memory whenever a new bit is added. The conversion is running perfectly without any issues, but when I try to allocate any more memory using malloc after the while loop, it is giving me segmentation fault when I call the same code a second time, first time it gives me no segmentation fault. I have checked and the "expBinary" is not storing anything out of bounds, I have given the code below

int binarySize = 0;                                                        
int * expBinary = malloc(sizeof(int));                                 
int i = 0;                                                                  

// Run until exp == 0                                                       
while(mpz_cmp_ui(exp,0) != 0)                                               
{                                                                           
    binarySize++;                                                           
    expBinary = (int*) realloc(expBinary,(binarySize));                     
    // Getting LSB of exp                                                   
    if(mpz_even_p(exp) != 0)                                                
        expBinary[i] = 0;                                                   
    else                                                                    
        expBinary[i] = 1;                                                                                                                                 
    // Incrmenting variables                                                
    i++;                                                                    
    // Dividing exponent by 2                                               
    mpz_tdiv_q_ui(exp,exp,2);                                               
}                                                                           

// This line is giving error
int * temp = malloc(sizeof(int));
8
  • 1
    Is this on linux? use valgrind Commented Jan 19, 2015 at 21:28
  • 1
    Please don't cast the result of realloc(). Commented Jan 19, 2015 at 21:28
  • Its in Mac (Unix) but I guess it will work the same in Linux. I removed the cast but its still showing the same error. Commented Jan 19, 2015 at 21:30
  • Yes valgrind does work on Mac. Commented Jan 19, 2015 at 21:31
  • @iharob without macports? please do tell or link. And may you live to a hundred healthy years if this pans out =) Commented Jan 19, 2015 at 21:33

1 Answer 1

5

If you are using an int array, then this is wrong

expBinary = (int*) realloc(expBinary,(binarySize));

it should be

expBinary = realloc(expBinary, binarySize * sizeof(*expBinary));

or equivalently,

expBinary = realloc(expBinary, binarySize * sizeof(int));

I prefer sizeof(*expBinary) for obvious reasons, and also, if realloc() fails you loose reference to the previous pointer so I recommend this

void *tmp;
tmp = realloc(expBinary, binarySize * sizeof(int));
if (tmp == NULL)
    handleFailureHereAndDontContinueToTheNextLineAndFree_expBinary_Please();
expBinary = tmp;

Now, if you want to print the representation using any printf("%s\n", expBinary); you should use char * instead, in that case you should consider that sizeof(char) == 1 always, and you will need one extra byte at the end of the 1's and 0's with the value '\0'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.