problem with malloc/realloc. Pls help.

Igor Peshansky pechtcha@cs.nyu.edu
Mon Aug 21 18:04:00 GMT 2006


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@cs.nyu.edu | igor@watson.ibm.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/



More information about the Cygwin mailing list