problem with malloc/realloc. Pls help.

Omololu prayerz.Omo@gmail.com
Mon Aug 21 16:13:00 GMT 2006


Hi,
 i have the following code. it compiles with gcc in Cygwin but the contents of
the array ifitQ that I get after the call to the subroutine readCharges2 is
gibberish.
The code compiles with gcc under Linux and it runs correctly. it also compiles
and runs correctly with windows visual studio. Pls help. The resuls i get with
Cygwin is:
isumNatms = 5 
rrr 13
rrr 14
rrr 15
rrr 16
*** 1628693268
*** 1628693268
*** 16
*** 1034

instead of:
isumNatms = 5 
rrr 13
rrr 14
rrr 15
rrr 16
*** 13
*** 14
*** 15
*** 16

the code is:

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

        void readCharges2(int *, int *, int *);
int main()
{
        static int *ifitQ;
        int *ipUniqAtms, *ipindexToFit;
                int j;
int x,y;
ipUniqAtms =&x;
ipindexToFit=&y;
               ifitQ = (int *) malloc(sizeof(int));
       if(ifitQ==NULL){printf("Unable to allocate matrix ifitQ\n");
               exit(EXIT_FAILURE);}
                readCharges2(ifitQ,ipUniqAtms,ipindexToFit);
       for(j=0; j< *ipUniqAtms ; j++)
       {
              printf("*** %d\n",ifitQ[j]);
       }

        return 0;
}

void readCharges2(int *ifitQ, int * ipUniqAtms, int * ipindexToFit)
{
       int  j, isumNatms=0;
       isumNatms=5;
       printf("isumNatms = %d \n",isumNatms);
       ifitQ = (int *) realloc(ifitQ,isumNatms*sizeof(int));
       if(ifitQ==NULL){printf("Unable to allocate matrix ifitQ\n");
               exit(EXIT_FAILURE);}

       ifitQ[0]=13;
       ifitQ[1]=14;
       ifitQ[2]=15;
       ifitQ[3]=16;
*ipUniqAtms =  4;
*ipindexToFit =  3;
       for(j=0; j< *ipUniqAtms ; j++)
       {
              printf("rrr %d\n",ifitQ[j]);
       }
}



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list