Runtime Error! . What's wrong? in my C code


  • 0
    M

    /* For the input
    "
    "0.9.9.9.9.9.9.9.9.9.9.9.9"
    "1.0", this generates Runtime Error!.
    Weird thing is that when I do "Run Code" everything looks working fine. with the exact answer..
    However, solution submission is coming up with the error message of 'Runtime Error'
    Working differently??

       #define max(a,b) ((a)>(b)?(a):(b))
        #define min(a,b) ((a)<(b)?(a):(b))
        void conv_version(char* version, int **versions, int* cntvalidnums)
        {
            int len = strlen(version);
            int i;
            int numidx = 0;
            char ch; 
            int digit;
            int vernum;
            vernum = 0;
            *versions = (int*)malloc(10*sizeof(int));
            for(i=0;i<len;i++)
            {
                ch=version[i];
                if(ch=='.')
                {
                    (*versions)[numidx] = vernum;
                    numidx++;
                    vernum = 0;
                    if(numidx%10==0)
                    {
                        realloc((*versions), ((numidx/10+1)*10)*sizeof(int));
                    }
                    continue;
                }
                digit = ch-'0';
                vernum = vernum*10+digit;
            }
            (*versions)[numidx]=vernum;
        //    printf("versions[%d]=%d \n",numidx, vernum);
            *cntvalidnums = numidx+1;
        }
        int cmpversions(int *versions1, int *versions2,int sz1, int sz2)
        {
            int i;
            int sz = min(sz1,sz2);
            printf("sz1=%d sz2=%d\n",sz1,sz2);
            for(i=0;i<sz;i++)
            {
                printf("v1[%d]=%d v2[%d]=%d\n",i,versions1[i],i,versions2[i]);
                if(versions1[i]>versions2[i])
                    return 1;
                else if(versions1[i]<versions2[i])
                    return -1;
                else
                {
                    continue;
                }
            }
            if(sz1 < sz2)
            {
                for(i=sz;i<sz2;i++)
                {
                    if(versions2[i])
                        return -1;
                }
                return 0;
            }
            else if(sz1 > sz2){
                for(i=sz;i<sz1;i++)
                {
                    if(versions1[i])
                        return 1;
                }
                return 0;
            }
            else
                return 0;
        }
        int compareVersion(char* version1, char* version2) {
            int resval;
        
            int **versions;
            versions = (int **)malloc(2*sizeof(int*));
        
            int cnt_valid_numbers1, cnt_valid_numbers2;
        
            conv_version(version1, &versions[0], &cnt_valid_numbers1);
            conv_version(version2, &versions[1], &cnt_valid_numbers2);
        
            resval = cmpversions(versions[0], versions[1], cnt_valid_numbers1, cnt_valid_numbers2);
            free(versions[0]);
            free(versions[1]);
            free(versions);
            return resval;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.