David
2010-Oct-06 14:53 UTC
[R] C code called from R: reallocate arrays passed as arguments?
Hello, I used to use a (somehow complicated) code in C and I have tried to adapt it to be called from R without making an excessive amount of changes. I am afraid I cannot post the code here, but I am going to try to describe the problem as accurately as possible. I have some arguments that are most of the cases required for internal C operations only; but as they are needed a few times as arguments, they are still passed as arguments. Another argument is a "switch" (with value 0 or 1) that states whether these "arguments" are really needed as arguments or not. When such switch is 0 (says "no"), the C code "feels free" to manipulate these "arguments", and in particular it tries to modify its extension (reallocate). To be clear: I define the "argument" in R: values1 <- double(size) values1 is passed as an argument in ".C". If the switch is set to 0 (says that "values1" is not a real argument), the C function tries to reallocate the pseudo-argument values1: double *tmp2 = realloc(values1, new_size); if(tmp2 != NULL){ values1 = tmp2; } else{ Rprintf("\nFATAL ERROR: Could not allocate memory for the array\n"); exit(2); } But then, when calling the function ".C", I get *** error for object ...: pointer being reallocated was not allocated Is there any workaround for this? Thank you very much in advance, David Can you please Cc to me any replies, just in case I may miss any of them among the whole amount of emails :-) ? [[alternative HTML version deleted]]
Duncan Murdoch
2010-Oct-06 15:05 UTC
[R] C code called from R: reallocate arrays passed as arguments?
On 06/10/2010 10:53 AM, David wrote:> Hello, > > I used to use a (somehow complicated) code in C and I have tried to adapt it > to be called from R without making an excessive amount of changes. I am > afraid I cannot post the code here, but I am going to try to describe the > problem as accurately as possible. > > I have some arguments that are most of the cases required for internal C > operations only; but as they are needed a few times as arguments, they are > still passed as arguments. Another argument is a "switch" (with value 0 or > 1) that states whether these "arguments" are really needed as arguments or > not. When such switch is 0 (says "no"), the C code "feels free" to > manipulate these "arguments", and in particular it tries to modify its > extension (reallocate).You should be using .Call rather than .C if you need to modify allocations. You should treat arrays passed via .C as fixed size. If you want to stick with the .C convention, then I think you'll need two calls: one to return the required sizes, and a second one with new vectors allocated at those sizes. It's often easier to learn how .Call works. Duncan Murdoch> To be clear: > > I define the "argument" in R: values1<- double(size) > values1 is passed as an argument in ".C". > If the switch is set to 0 (says that "values1" is not a real argument), the > C function tries to reallocate the pseudo-argument values1: > > double *tmp2 = realloc(values1, new_size); > if(tmp2 != NULL){ > values1 = tmp2; > } > else{ > Rprintf("\nFATAL ERROR: Could not allocate memory for the > array\n"); > exit(2); > } > > But then, when calling the function ".C", I get *** error for object ...: > pointer being reallocated was not allocated > > Is there any workaround for this? > > Thank you very much in advance, > > David > Can you please Cc to me any replies, just in case I may miss any of them > among the whole amount of emails :-) ? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.