[R-pkg-devel] [External] SETLENGTH()

Vladimir Dergachev vo|ody@ @end|ng |rom m|nd@pr|ng@com
Sun May 5 03:45:01 CEST 2024



On Sat, 4 May 2024, luke-tierney using uiowa.edu wrote:

> On Sat, 4 May 2024, Vladimir Dergachev wrote:
>
>> [Some people who received this message don't often get email from 
>> volodya using mindspring.com. Learn why this is important at 
>> https://aka.ms/LearnAboutSenderIdentification ]
>> 
>> I noticed a note on RMVL package check page for development version of R:
>> 
>> Found non-API call to R: ?SETLENGTH?
>> 
>> Is this something that is work-in-progress for the development version, or
>> has SETLENGTH() been deprecated ? What should I use instead ?
>
> SETLENGTH has never been part of the API. It is not safe to use except
> in a very, very limited set of circumstances. Using it in other
> settings will confuse the memory manager, leading at least to
> mis-calculation of memory use information and possibly to
> segfaults. For most uses I have seen, copying to a new vector of the
> right size is the only safe option.
>
> The one context where something along these lines might be OK is for
> growable vectors. This concept is emphatically not in the API at this
> point, and the way it is currently implemented in base is not robust
> enough to become an API (even though some packages have used it). It
> is possible that a proper API for this will be added; at that point
> SETLENGTH will be removed from the accessible entry points on
> platforms that allow this.
>
> So if you are getting a note about SETLENGTH, either stop using it or
> be prepared to make some changes at fairly short notice.
>
> [Similar considerations apply to SET_TRUELENGT. In most but not all
> cases using it is less dangerous, but you should still look for other
> options if you want your code to continue to work.]

Great, thank you for the explanation ! I will rewrite the code to not use 
SETLENGTH().

My use case was to allocate a vector of some size N_max and then 
repeatedly populate it with variable number of elements. Since the vector 
was protected during the loop, I would have expected to save on memory 
allocation calls.

best

Vladimir Dergachev

>
> Best,
>
> luke
>
>> 
>> thank you very much
>> 
>> Vladimir Dergachev
>> ______________________________________________
>> R-package-devel using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>> 
>
> -- 
> Luke Tierney
> Ralph E. Wareham Professor of Mathematical Sciences
> University of Iowa                  Phone:             319-335-3386
> Department of Statistics and        Fax:               319-335-3017
>   Actuarial Science
> 241 Schaeffer Hall                  email:   luke-tierney using uiowa.edu
> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
>



More information about the R-package-devel mailing list