From pdalgd at gmail.com  Wed Mar 19 23:17:13 2014
From: pdalgd at gmail.com (peter dalgaard)
Date: Wed, 19 Mar 2014 23:17:13 +0100
Subject: [Rd] Memcheck: Invalid  read of size 4
In-Reply-To: <532A1314.8040206@u-paris10.fr>
References: <532A1314.8040206@u-paris10.fr>
Message-ID: <373A4631-6C57-493C-A347-0BAB256C507A@gmail.com>


On 19 Mar 2014, at 22:58 , Christophe Genolini <cgenolin at u-paris10.fr> wrote:

> Hi the list,
> 
> One of my package has a memory issue that I do not manage to understand. The Memtest notes is here:
> <http://www.stats.ox.ac.uk/pub/bdr/memtests/valgrind/kml-Ex.Rout>
> 
> Here is the message that I get from Memtest
> 
> --- 8< ----------------
> ~ Fast KmL ~
> ==27283== Invalid read of size 4
> ==27283==    at 0x10C5DF28: kml1 (kml.c:183)
> ...
> ==27283==    by 0x10C5DE4F: kml1 (kml.c:151)
> ...
> ==27283==    at 0x10C5DF90: kml1 (kml.c:198)
> --- 8< ----------------
> 
> 
> Here is the function kml1 from the file kml.c (I add some comments to tag the lines 151, 183 and 198)
> 
> --- 8< ----------------
> void kml1(double *traj, int *nbInd, int *nbTime, int *nbClusters, int *maxIt, int *clusterAffectation1, int *convergenceTime){
> 
>    int i=0,iter=0;
>    int *clusterAffectation2=malloc(*nbInd * sizeof(int));                      // lines 151
>    double *trajMean=malloc(*nbClusters * *nbTime * sizeof(double));
> 
>    for(i = 0; i < *nbClusters * *nbTime; i++){trajMean[i] = 0.0;};
>    for(i = 0; i < *nbInd; i++){clusterAffectation2[i] = 0;};
> 
>    for(iter = 0; iter < *maxIt; iter+=2){
> 	calculMean(traj,nbInd,nbTime,clusterAffectation1,nbClusters,trajMean);
>       	affecteIndiv(traj,nbInd,nbTime,trajMean,nbClusters,clusterAffectation2);
> 
> 	i = 0;
> 	while(clusterAffectation1[i]==clusterAffectation2[i] && i <*nbInd){i++;}; // lines 183
> 	if(i == *nbInd){
> 	    *convergenceTime = iter + 1;
> 	    break;
> 	}else{};
> 
> 	calculMean(traj,nbInd,nbTime,clusterAffectation2,nbClusters,trajMean);
> 	affecteIndiv(traj,nbInd,nbTime,trajMean,nbClusters,clusterAffectation1);
> 
> 	i = 0;
> 	while(clusterAffectation1[i]==clusterAffectation2[i] && i<*nbInd){i++;}; // lines 198
> 	if(i == *nbInd){
> 	    *convergenceTime = iter + 2;
>      	    break;
> 	}else{};
>    }
> }
> --- 8< ----------------
> 
> Do you know what is wrong in my C code?

Yes. You need to reverse operands of &&. Otherwise you'll be indexing with i==*nbind before finding that (i < *nbind) is false. 

> Thanks
> 
> Christophe
> 
> -- 
> Christophe Genolini
> Ma?tre de conf?rences en bio-statistique
> Universit? Paris Ouest Nanterre La D?fense
> INSERM UMR 1027
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] Memcheck: Invalid read of size 4
peter dalgaard