0

I am running into a segmentation fault error in my code and I am not sure what may be causing it. What is even more odd is that when I run I compile/run in gcc-4.4.6 RH6 I obtain no error, but on other compilers/linux distros I get a segmentation fault error.

Here is a snippet of the part of the code that I think may be generating the issue:

int BIN_SIZE=(2*width)/bins;
//binCounts and binCounts2 store the fragment counts in each bin. mask=1 flags histone modification site

float **binVals;

binVals = (float **)malloc(chromNum*sizeof(int *));
//Initialize the arrays
totalBinNum = 0;

for (i=0;i<chromNum;i++)
{
    totalBinNum += chromInfo[i].chromSize/BIN_SIZE+1;
    binVals[i] = (float *)malloc((chromInfo[i].chromSize/BIN_SIZE+1)*sizeof(float));
    memset(binVals[i], 0, (chromInfo[i].chromSize/BIN_SIZE+1)*sizeof(float));
}

If you know some easy catch on what may be causing the error please let me know? Otherwise it could also be in some other part of the code leading to not a smart Q :(

Dnaiel
  • 7,622
  • 23
  • 67
  • 126
  • 6
    **[Do not cast the return value of `malloc()`!](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)** –  Jul 15 '13 at 22:12
  • 3
    `malloc(chromNum*sizeof(int *));` I fail to see the `int*` here. – wildplasser Jul 15 '13 at 22:19
  • Which other compiler versions? Newer or older than GCC 4.4.6? – Jonathan Leffler Jul 15 '13 at 22:19
  • 1
    Not a direct answer, but on Linux it's easy to compile with debug info ('-g'), run your program with GDB, and use the "backtrace" command (if needed). I'd try that, because it's a lot easier to figure out what you did wrong if you know what line the error occurs on. :) – David Duncan Jul 15 '13 at 22:20
  • 1
    have you tried valgrind? – perreal Jul 15 '13 at 22:20
  • So far I tried gcc-4.4.4, gcc-4.5.3 and gcc-4.7.0 (all failed) – Dnaiel Jul 15 '13 at 22:21
  • 2
    Not that I don't trust your code, but the most likely place I see a segmentation fault could occur is when your `chromInfo` wasn't initialized correctly with size `chromNum`. – cheeyos Jul 15 '13 at 22:28
  • Do you check for memory allocation errors? You don't show that...You may have been accidentally hitting a compiler bug in 4.4.6 (so your faulty code runs OK there) that was not in 4.4.4 and was fixed in 4.5.x and above. – Jonathan Leffler Jul 15 '13 at 22:28
  • 1
    `BIN_SIZE=(2*width)/bins` and `totalBinNum += chromInfo[i].chromSize/BIN_SIZE+1;` I got the feeling there is some sloppy bookkeeping going on here; the +1's are added to correct for the moduli not being zero. The **ALL-CAPS** variable and the signed sizes&counts are not a good sign either. IMnsvHO ... – wildplasser Jul 15 '13 at 22:30
  • I am glad you closed it, it ended up being a very bad question, sorry about that, thanks. – Dnaiel Jul 16 '13 at 14:04

1 Answers1

1

It would be more precise to do so:

binVals = malloc(chromNum*sizeof(float *));

But it is not likely that this is the cause of the error, as you can expect that 2 pointers, even if to different types int* and float*, will have the same size. In short, the source of the error is probably somewhere else in your code.

Some other suggestions:

  1. I would suggest removing the other type cast in the other malloc.
  2. I would use some temporary variable to store chromInfo[i].chromSize/BIN_SIZE+1, so that you do not have to repeat the expression 3 times with very likely cut and past errors.
  3. You can compact the malloc and the memset to zero in one calloc call.
Antonio
  • 19,451
  • 13
  • 99
  • 197