[Rd] Memcheck: Invalid read of size 4

Christophe Genolini cgenolin at u-paris10.fr
Wed Mar 19 22:58:44 CET 2014


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

Christophe

-- 
Christophe Genolini
Maître de conférences en bio-statistique
Université Paris Ouest Nanterre La Défense
INSERM UMR 1027



More information about the R-devel mailing list