<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.00.2314.1000" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000>Hello:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>I am looking at 
"Writing R Extensions", subsection 3.6.4, using R 1.1.0, under NT4.0,&nbsp;with 
VC++ 6.0.&nbsp; Under these conditions I can&nbsp;use the first method given for 
coding the out() function, but not the second, which uses getAttrib() and 
setAttrib().&nbsp; I hope that someone&nbsp;will&nbsp;tell me how this second 
method can also be made to work.&nbsp; Details follow, for anyone&nbsp;kind 
enough to help.&nbsp; I include the actual code lifted from the manual, so that 
you can compile, dyn.load() and tell me that it works fine for 
you!</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>The R 
function&nbsp;of interest is&nbsp;the outer product of two 
vectors:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>out &lt;- 
function(x, y) .Call("out", as.double(x), as.double(y))</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Define out() as 
simply (straight from the text):</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=ti size=2><SPAN class=642084208-12072000>SEXP out(SEXP x, SEXP 
y)<BR>{<BR>&nbsp;int i, j, nx, ny;<BR>&nbsp;double tmp;<BR>&nbsp;SEXP 
ans;<BR>&nbsp;nx = length(x); ny = length(y);<BR>&nbsp;PROTECT(ans = 
allocMatrix(REALSXP, nx, ny));<BR>&nbsp;for(i = 0; i &lt; nx; i++) 
{<BR>&nbsp;&nbsp;tmp = REAL(x)[i];<BR>&nbsp;&nbsp;for(j = 0; j &lt; ny; 
j++)<BR>&nbsp;&nbsp;&nbsp;REAL(ans)[i + nx*j] = tmp * 
REAL(y)[j];<BR>&nbsp;}<BR>&nbsp;UNPROTECT(1);<BR>&nbsp;return(ans);<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial><SPAN class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Compiling, I get a 
correct answer,</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=ti size=2><SPAN class=642084208-12072000>&gt; 
out(c(1,2),c(3,4))</SPAN></FONT></DIV>
<DIV><SPAN class=642084208-12072000></SPAN>&nbsp;</DIV>
<DIV><SPAN class=642084208-12072000><FONT face=Arial 
size=2>&nbsp;&nbsp;&nbsp;&nbsp; [,1] [,2]<BR>[1,]&nbsp;&nbsp;&nbsp; 
3&nbsp;&nbsp;&nbsp; 4<BR>[2,]&nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp;&nbsp; 
8<BR></FONT></DIV></SPAN>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>All is well.&nbsp; 
The following function (second method with lines commented) also 
works:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=ti size=2><SPAN class=642084208-12072000>SEXP out(SEXP x, SEXP 
y)<BR>{<BR>&nbsp;int i, j, nx, ny;<BR>&nbsp;double tmp;<BR>&nbsp;SEXP ans, dim, 
dimnames;<BR>&nbsp;nx = length(x); ny = length(y);<BR>&nbsp;PROTECT(ans = 
allocVector(REALSXP, nx*ny));<BR>&nbsp;for(i = 0; i &lt; nx; i++) 
{<BR>&nbsp;&nbsp;tmp = REAL(x)[i];<BR>&nbsp;&nbsp;for(j = 0; j &lt; ny; 
j++)<BR>&nbsp;&nbsp;&nbsp;REAL(ans)[i + nx*j] = tmp * 
REAL(y)[j];<BR>&nbsp;}<BR>&nbsp;PROTECT(dim = allocVector(INTSXP, 
2));<BR>&nbsp;INTEGER(dim)[0] = nx; INTEGER(dim)[1] = 
ny;<BR>//&nbsp;setAttrib(ans, R_DimSymbol, dim);<BR>&nbsp;PROTECT(dimnames = 
allocVector(VECSXP, 2));<BR>//&nbsp;VECTOR(dimnames)[0] = getAttrib(x, 
R_NamesSymbol);<BR>//&nbsp;VECTOR(dimnames)[1] = getAttrib(y, 
R_NamesSymbol);<BR>//&nbsp;setAttrib(ans, R_DimNamesSymbol, 
dimnames);<BR>&nbsp;UNPROTECT(3);<BR>&nbsp;return(ans);<BR>}<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Here is the output, 
a vector:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>&gt; 
out(c(1,2),c(3,4))<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>[1] 3 6 4 
8</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>But when I uncomment 
any one of the 4 lines which use getAttrib() or setAttrib(), R crashes for me, 
with a Windows message box.&nbsp; Uncommenting the first such line, for example, 
crashes R, with a message box:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>The instruction at 
"0x&lt;HEX address&gt;" refernced memory at "0x&lt;another address&gt;".&nbsp; 
The memory could not be "read".</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Is there anything I 
can do differently to make these attribute functions work?&nbsp; thanks in 
advance for your help.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Tom Richards</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=642084208-12072000></SPAN></FONT>&nbsp;</DIV></BODY></HTML>