From tlumley at uw.edu  Mon Oct 24 18:36:53 2011
From: tlumley at uw.edu (Thomas Lumley)
Date: Mon, 24 Oct 2011 09:36:53 -0700
Subject: [Rd] C function is wrong under Windows 7
In-Reply-To: <CAHXx1xOMrWpv63a85aOASXQD2yBQa01sL-4-+25UDhv0SkAaOA@mail.gmail.com>
References: <CAHXx1xOMrWpv63a85aOASXQD2yBQa01sL-4-+25UDhv0SkAaOA@mail.gmail.com>
Message-ID: <CAJ55+dKZmKfzdpFeo0NEOtceTER=5dPFM1QQaQBofe2yef=W7g@mail.gmail.com>

On Mon, Oct 24, 2011 at 6:04 AM, Evarist Planet
<evarist.planet at irbbarcelona.org> wrote:
> Dear mailing list,
>
> I have a C function that gives me a wrong result when I run it under Windows
> 7.

The fact that you extract pointers to the contents of fchr and sign
before coercing them to REALSXP is deeply suspicious to me, though I
admit I don't see how it causes the problem, since you don't use the
new versions, you don't overwrite them, and the old versions should
still be protected as arguments to the function.

> SEXP getEs(SEXP fchr, SEXP sign)
> {
> ?int i, nfchr, nsign;
> ?double *rfchr = REAL(fchr), *res;
> ?int *rsign = INTEGER(sign);
>
> ?PROTECT(fchr = coerceVector(fchr, REALSXP));
> ?PROTECT(sign = coerceVector(sign, REALSXP));
>
> ?nfchr = length(fchr);
> ?nsign = length(sign);
>
> ?SEXP es;
> ?PROTECT(es = allocVector(REALSXP, nfchr));
> ?res = REAL(es);
>
> ?double nr = getNr(rfchr, rsign, nsign);
>
> ?SEXP phit;
> ?PROTECT(phit = allocVector(REALSXP, nfchr));
> ?double *rphit;
> ?rphit = REAL(phit);
> ?for(i = 0; i < nfchr; i++) rphit[i] = 0.0;
> ?getPhit(rfchr, rsign, nsign, nr, rphit);
> ?cumsum(rphit, nfchr);
>
> ?SEXP pmiss;
> ?PROTECT(pmiss = allocVector(REALSXP, nfchr));
> ?double *rpmiss;
> ?rpmiss = REAL(pmiss);
> ?for(i = 0; i < nfchr; i++) rpmiss[i] = 0.0;
> ?getPmiss(rsign, nfchr, nsign, rpmiss);
> ?cumsum(rpmiss, nfchr);
>
> ?for (i = 0; i < nfchr; ++i) {
> ? res[i] = rphit[i] - rpmiss[i];
> ? }
>
> ?UNPROTECT(5);
> ?return es;
> }
>
> Could you please help me to find out what I am doing wrong?
>
> Many thanks in advance,
>
> --
> Evarist Planet
> Research officer, Bioinformatics and Biostatistics unit
> IRB Barcelona
> Tel (+34) 93 402 0553
> Fax (+34) 93 402 0257
>
> evarist.planet at irbbarcelona.org
> http://www.irbbarcelona.org/bioinformatics
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



[Rd] C function is wrong under Windows 7
Thomas Lumley