#include <stdio.h>
#include <stdlib.h>

struct amicable
{
	int** amicablePair;
	int size;
};

struct amicable *getAmicablePairs(int startnum, int endnum);
int sumFactors(int number);

int main(int argc, char** argv) {
    int startnum = 220;
    int endnum = 284;
    struct amicable* ami;
    int i;
    ami = getAmicablePairs(startnum, endnum);
    if(ami==NULL)
    {
        printf("Error: malloc could not allocate");
        return (EXIT_SUCCESS);
    }
    
    printf("{");

    for(i = 0; i<ami->size; i++)
    {
	printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
    }
    printf("}");
     
    //getch();
    //return 0;
    return (EXIT_SUCCESS);
}

int sumFactors(int number){
    int i;
    int sum;
    sum=1;

    for(i=2;i<=number/2;i++){
        if(number%i==0)
            {
                sum=sum+i;
            }
    }
    return sum;
}

struct amicable *getAmicablePairs(int startnum, int endnum)
{   
    int nrows=(endnum-startnum);
    int ncols=2;
        
    int i,j,k=0;
    
    struct amicable* record;

    /*  allocate array of pointers  */
    record->amicablePair=(int**)malloc(nrows*sizeof(int*));
    if(record->amicablePair==NULL)
  {
     printf("Error: malloc could not allocate %d bytes for a\n", 2 * sizeof(int *));
     return record;
  }
    /*  allocate each row  */
     for(i=0;i<nrows;i++) {
          record->amicablePair[i]=(int*)malloc(ncols*sizeof(int));
          if(record->amicablePair[i]==NULL)
          {
            printf("Error: malloc could not allocate %d bytes for a\n", 3 * sizeof(int));
            return record;
         }
     }

     /* problem solution */
    for(i = startnum; i <= endnum; i++)
    {
        for(j = endnum; j >= startnum; j--)
        {
            if ((sumFactors(i)==j) && (sumFactors(j)==i) && (i != j))
            {
                record->amicablePair[k][0]=i;
                record->amicablePair[k][1]=j;
                k++;
            }
        }
    }
   
    record->size=k;    
    return record;
}

I am getting this error while executing (in netbeans 6.7) the above code:

6 [main] asp11d 3400 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
    908 [main] asp11d 3400 open_stackdumpfile: Dumping stack trace to asp11d.exe.stackdump

RUN FAILED (exit value 35,584, total time: 421ms)

..............please send me solutions as soon as possible.................
Thanks in advance

struct amicable* record;

    /*  allocate array of pointers  */
    record->amicablePair=(int**)malloc(nrows*sizeof(int*));

record needs to point to something before you dereference what it points to with the -> operator.

struct amicable* record;

    /*  allocate array of pointers  */
    record->amicablePair=(int**)malloc(nrows*sizeof(int*));

record needs to point to something before you dereference what it points to with the -> operator.

if u means that 1st I have to use any dummy value like this:

record->amicablePair=0;

OR

struct amicable
{
int** amicablePair=0;
int size;
};

then

/*  allocate array of pointers  */
    record->amicablePair=(int**)malloc(nrows*sizeof(int*));

Nope. More like:

struct amicable *record;

/* record must point to some memory before using it */
record =  &a_struct_amicable_block_of_memory; /* can be done with malloc */

Nope. More like:

struct amicable *record;

/* record must point to some memory before using it */
record =  &a_struct_amicable_block_of_memory; /* can be done with malloc */

I have tried using this way:

struct amicable *record;
struct amicable amic;
/* record must point to some memory before using it */
record =  &amic;

and receive the same error:

6 [main] asp11f 2528 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
1067 [main] asp11f 2528 open_stackdumpfile: Dumping stack trace to asp11f.exe.stackdump

RUN FAILED (exit value 35,584, total time: 422ms)

I have tried using this way:

struct amicable *record;
struct amicable amic;
/* record must point to some memory before using it */
record =  &amic;

and receive the same error:

6 [main] asp11f 2528 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
1067 [main] asp11f 2528 open_stackdumpfile: Dumping stack trace to asp11f.exe.stackdump

RUN FAILED (exit value 35,584, total time: 422ms)

Also I have seen that I am getting the correct address in "ami" and getting the run time error in below code:

ami = getAmicablePairs(startnum, endnum);

if(ami==NULL)
{
printf("Error: malloc could not allocate");
return (EXIT_SUCCESS);
}
//till this everything is ok, "ami" contains the address of a "struc amicable" block
//after that when I am trying to do ami->Size....I am getting the error
printf("{");
for(i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
}
printf("}");


here is few testing and its result:
/* inside *getAmicablePairs(int startnum, int endnum) */
k:= 2
address of k:= 2280572
record:= 2280560
record->size:= 2
/* inside main() */
ami:= 2280560
ami->size:= 4199812 (Size of array is 2 not 4199812)

for(i = 0; i<ami->size; i++)
    {
    printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
    }

in place of ami->size try with (*ami)->size....May be that will do..

using namespace std;
#include <iostream>

int factsum(int num)
{
int sum=1;int rem; // sum of factors of a number excluding the number itself
f

commented: This thread is almost a year old. +0
commented: Nope -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.