Runtime Memory Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itxharsh
    New Member
    • Nov 2009
    • 4

    Runtime Memory Error

    The error during run time is:"Your code has stopped its execution with a non-zero (failure) exit value.This is generally due to run time Exceptions like Memory Access Violation and Floating Point Exception. Please check your code for run time Exceptions and try again.". What could be the error? Please help.
    [CODE=C]
    #include<conio. h>
    #include<stdio. h>
    struct amicable
    {
    int **amicablePairs ;
    int size;
    };
    struct amicable* getAmicablePair s(int startnum,int endnum);
    int main()
    {
    struct amicable *ami;
    int i,startnum,endn um;
    clrscr();

    ami=getAmicable Pairs(100,2000) ;
    printf("{");
    for(i=0;i<ami->size;i++)
    {
    printf("{%d %d}",ami->amicablePair s[i][0],ami->amicablePair s[i][1]);
    }
    printf("}");
    getch();
    return 0;
    }


    struct amicable * getAmicablePair s(int startnum,int endnum)
    {
    struct amicable *amic;
    int i,j,divisor=0,d ivisor1=0,k=0,l ,**p;
    p=malloc(15*siz eof(int*));
    if(p==NULL)
    printf("failed for p");
    if(startnum>0&& endnum>0&&start num<endnum&&end num<=15000)
    {
    for(i=startnum; i<=endnum;i++)
    {
    for(j=1;j<=i/2;j++)
    {
    if(i%j==0)
    divisor+=j;
    }
    if(divisor<=end num&&divisor>i)
    {
    for(l=1;l<=divi sor/2;l++)
    {
    if(divisor%l==0 )
    divisor1+=l;
    }
    if(divisor1==i)
    {
    p[k]=malloc(2*sizeo f(int));
    if(p[k]==NULL)
    printf("failed for loop");
    p[k][0]=i;p[k][1]=divisor;
    //amic->amicablePair s[k][0]=i;
    //amic->amicablePair s[k][1]=divisor;
    k++;
    }
    }
    divisor=0;divis or1=0;
    }
    if(k==1)
    goto here;
    amic->size=k;
    amic->amicablePairs= p;
    return amic;
    }
    else
    here:return NULL;
    }
    [/CODE]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You have a limit to your malloc memory of 15 but you never ensure that k does not exceed this limit.

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      Amic is not initialized in the function and points nowhere, but then you write to its members.

      Comment

      Working...