delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/08/21/12:01:27

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Omololu <prayerz DOT Omo AT gmail DOT com>
Subject: problem with malloc/realloc. Pls help.
Date: Mon, 21 Aug 2006 15:25:42 +0000 (UTC)
Lines: 75
Message-ID: <loom.20060821T171635-299@post.gmane.org>
Mime-Version: 1.0
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: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
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 <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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019