-1

I work with char ****pCourses

int Courses(char ****pCourses, int ****pGrades, int **pCount, int *count)
{
    char *buffer=NULL;
    char letter;
    int j=0, i;
    int size=20;
    int countCourse=0, sumCourses=0;
    buffer=(char *)malloc(size*sizeof(char));
    do
    {
        scanf("%c",&letter);
    }while(letter==32);
    while(letter!=10)
    {
        while(letter!=',')
        {
            //in case we need to expend the buffer
            if(j>size)
            {
                size*=2;
                buffer=(char *)realloc(buffer,size*sizeof(char));
            }
            buffer[j]=letter;
            j++;
            //The new letter from the name of course
            scanf("%c",&letter);
        }
        //The end of the name of course
        buffer[j]='\0';
        j=0;
        if(countCourse==0)
        {
            *pCount=(int *)realloc(*pCount, ((*count)+1)*sizeof(int));
            (*pCount)[*count]=1;
        }
        else
            (*pCount)[*count]++;
        //Add the course's name to the system
        *pCourses=(char ***)realloc(*pCourses, ((*count)+1)*sizeof(char ***));
        (*pCourses)[*count]=(char **)realloc((*pCourses)[*count],(countCourse+1)*sizeof(char *));
        ((*pCourses)[*count])[countCourse]=(char *)malloc(strlen(buffer)+1);
        strcpy(((*pCourses)[*count])[countCourse], buffer);
        countCourse++;
        scanf("%c",&letter);
    }
    free(buffer);
    return 0;
}

while running I get a problem because of the next line:

(*pCourses)[*count]=(char **)realloc((*pCourses)[*count],(countCourse+1)*sizeof(char *));

that leads me to this part (from dbgheap.c):

{
    while (nSize--)
    {
        if (*pb++ != bCheck)
        {
            return FALSE;
        }
    }
    return TRUE;

}

does someone know what it means or why does this happen?

  • 4
    Someone told me not to be a three-star programmer. I think he missed you. :) – Sourav Ghosh Dec 21 '15 at 14:44
  • First of all, don't assign back to the same pointer you use in the call to `realloc`, think about what would happen if `realloc` returned `NULL`. Secondly, [in C you should not cast the return of `malloc` (or it siblings like `realloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (or any function returning `void *`). Thirdly, if you know the exact line where the crash happen, why don't you check the values of the involved variables to see that they are all valid? – Some programmer dude Dec 21 '15 at 14:48
  • 2
    Lastly, can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? Just seeing those two lines of code doesn't tell us much. – Some programmer dude Dec 21 '15 at 14:49
  • I added the full function, generaly the problem is with that line, but i dont know what is wrong because it seems logic. – Tamir Buskila Dec 21 '15 at 14:58
  • With which arguments did you call the function? What is fed from standard input? – Armali Jan 10 '18 at 07:41

1 Answers1

0

I think you've already been told what the problem is. All the alloc-type functions can fail and you should test for that and ideally write code that is robust enough to code with a failure.

Note that repeatedly using realloc() is not a good idea. If you expect to have to repeatedly add ( or remove ) data then you should be using a different structure, like a linked list or tree, which are designed to allow adding and removing data on the fly.