X-Spam-Check-By: sourceware.org To: cygwin AT cygwin DOT com From: Omololu Subject: problem with malloc/realloc. Pls help. Date: Mon, 21 Aug 2006 15:25:42 +0000 (UTC) Lines: 75 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit User-Agent: Loom/3.14 (http://gmane.org/) X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com 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 #include 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/