delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/08/21/14:03:03

X-Spam-Check-By: sourceware.org
Date: Mon, 21 Aug 2006 14:02:46 -0400 (EDT)
From: Igor Peshansky <pechtcha AT cs DOT nyu DOT edu>
Reply-To: cygwin AT cygwin DOT com
To: Omololu <prayerz DOT Omo AT gmail DOT com>
cc: cygwin AT cygwin DOT com
Subject: Re: problem with malloc/realloc. Pls help.
In-Reply-To: <loom.20060821T171635-299@post.gmane.org>
Message-ID: <Pine.GSO.4.63.0608211358540.25747@access1.cims.nyu.edu>
References: <loom DOT 20060821T171635-299 AT post DOT gmane DOT org>
MIME-Version: 1.0
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
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

On Mon, 21 Aug 2006, Omololu wrote:

> 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.

This is expected behavior.  Read on.

> 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:

Ouch.  Indentation issues aside, you have a bug in this program.

> #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);

Note that ifitQ is passed *by value*, and any changes you make to it
inside readCharges2() will be lost when you come back to main.  So, you
realloc it inside readCharges2(), and store values in the new array, but
in main(), you print the values in the *old* array (which are, as you
said, gibberish, as the memory has been released to the system).

>        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]);
>        }
> }

The reason it probably worked on Linux is because in Linux, realloc will
try hard to keep the array at the same address if it can simply bump the
size of the pointer.  Cygwin's realloc doesn't.
HTH,
	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_	    pechtcha AT cs DOT nyu DOT edu | igor AT watson DOT ibm DOT com
ZZZzz /,`.-'`'    -.  ;-;;,_		Igor Peshansky, Ph.D. (name changed!)
     |,4-  ) )-,_. ,\ (  `'-'		old name: Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"Las! je suis sot... -Mais non, tu ne l'es pas, puisque tu t'en rends compte."
"But no -- you are no fool; you call yourself a fool, there's proof enough in
that!" -- Rostand, "Cyrano de Bergerac"

--
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