[Rd] Memcheck: Invalid read of size 4

peter dalgaard pdalgd at gmail.com
Thu Mar 20 18:01:18 CET 2014


On 20 Mar 2014, at 16:56 , Christophe Genolini <cgenolin at u-paris10.fr> wrote:

> Thanks a lot. Your correction works just fine.
> 
> Any idea of what goes wrong for the line 151, which is
> 
>   int *clusterAffectation2=malloc(*nbInd * sizeof(int));                      // lines 151
> 

Nothing. It's just that memcheck marks the point of allocation for you: There's a discrepancy between what you allocate and what you access, but it can't really tell whether the allocation was too short or the access steps past the end.

-pd

> 
> 
> 
>> 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
> 
> 
> -- 
> Christophe Genolini
> Maître de conférences en bio-statistique
> Université Paris Ouest Nanterre La Défense
> INSERM UMR 1027

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



More information about the R-devel mailing list