Thread: calloc/malloc problem

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10

    calloc/malloc problem

    this is a unit module of a bigger program , which does permutations, the idea is to return a multidimensional array of the permutation results..but I'm getting a segmentation fault when allocation space to store the results..
    can someone please tell me what I'm doing wrong?

    Code:
    int** permConstP(int *a, int  arrStInd, int  arrEndInd)
    {
        static int ** baseColours;
       static int bcInd= 0;
      
        //internal swap function
        void swap(int *x, int *y)
        {
            int temp;
            temp = *x;
            *x = *y;
            *y = temp;
        }
        
       if (arrStInd == arrEndInd)
         {
             baseColours[bcInd]=(int*)calloc(5, sizeof(int)); // getting a segmentation fault here in debugger
            for(int i = 0; i <= arrEndInd ; i++)
            {
                //printf("%d ",a[i]);
                baseColours[bcInd][i]=a[i];
            }
            bcInd++;
         }
       else
       {
           for (int i = arrStInd; i <= arrEndInd; i++)
           {
              swap((a+arrStInd), (a+i));
              permConstP(a, arrStInd+1, arrEndInd,&baseColours, &bcInd); //backtrack
              swap((a+arrStInd), (a+i));
           }
       }
       return baseColours;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    1. Why is your basecolours static.
    2. Standard C doesn't have nested functions.
    3. Having <= suggests off by 1 counting.
    4. Why the constant 5 in your calloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    Quote Originally Posted by Salem View Post
    1. Why is your basecolours static.
    2. Standard C doesn't have nested functions.
    3. Having <= suggests off by 1 counting.
    4. Why the constant 5 in your calloc.
    int ** baseColours is static because the function does backtracking...I need baseColours to be the same no matter how many times the function is called..
    2.well I'm not using standard c, I'm using c99 or higher...
    3.that was done on purpose, there is no problem there...
    4.constant five in my calloc because I know I need 5 *(sizeof(int)) to store 5 int values..
    still I have no answer to the problem...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    1. No version of standard C has nested functions. Try using std=c99 rather than whatever gcc lets you get away with (Using the GNU Compiler Collection (GCC): Nested Functions).

    2. constant five in my calloc because I know I need 5 *(sizeof(int)) to store 5 int values..
    So you're absolutely sure that arrEndInd is always <= 4?

    Another thing you missed is allocating baseColours to begin with.

    You need
    baseColours = malloc( n * sizeof(int*));
    before you start doing
    baseColours[x] = malloc( 5 * sizeof(int));

    I guess the next step is figuring out how to use a debugger, so you get an accurate picture of which line crashed, and the state of variables at that point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc ,calloc code problem..
    By transgalactic2 in forum C Programming
    Replies: 37
    Last Post: 10-26-2008, 11:43 AM
  2. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  3. Malloc vs. Calloc
    By FCF in forum C Programming
    Replies: 13
    Last Post: 06-30-2002, 06:41 PM
  4. Calloc vs. Malloc
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 03:56 PM

Tags for this Thread