Skip to content

No realloc warning when adding many inside a function #1633

Description

@mattdowle
packageVersion("data.table")
# [1] ‘1.17.99’
x = data.table(a=1:3)
ncol = truelength(x)
ncol
# [1] 1025

f = function(DT) {
 for (i in seq_len(ncol + 100L)) {
   DT[,paste0("new",i):=1:3]
 }
}
f(x)            # I thought there was a warning here that := on i==ncol did a realloc
dim(x)
# [1]   3 1025       # wrong result

f = function(DT) {
  DT[, paste0("new", seq_len(ncol + 100L)) := 1:3]
  NULL
}
f(x)
dim(x)
# [1]   3 1025       # wrong result

f = function(DT) {
 for (i in seq_len(ncol + 100L)) {
   DT[,paste0("new",i):=1:3]
 }
 DT                            # needed in this case
}
x = f(x)                       # x= needed in this case
dim(x)
# [1]   3 1126                      # correct result

x = data.table(a = 1:3)
f = function(DT) {
  for (j in seq_len(ncol + 100L)) set(DT, NULL, paste0("new", j), 1:3)
}
f(x)
# Error in set(DT, NULL, paste0("new", j), 1:3) : 
#   Internal error in assign: input dt has not been allocated enough column slots. l=1025, tl=1025, adding 1. Please report to the data.table issues tracker.
# dim(x)

I seem to remember there was a warning. Maybe it isn't happening in this case for some reason. Need to reinstate the warning and add a test for it.

Activity

  1. changed the title [-]No realloc warning when adding many columns in a loop, inside a function[/-] [+]No realloc warning when adding many inside a function[/+] on Jul 5, 2025
  2. ProjectsByFerm commented on Mar 25, 2026

    @ProjectsByFerm

    Hi there, I am currently in an Open Source Software class, and my team and I are interested in this issue. Is this issue still open/important?

  3. venom1204 commented on Jun 13, 2026

    @venom1204
    Contributor

    hi @tdhock @joshhwuu
    i looked into the issue and planning to restore a clear warning when reaclloc happens inside functions.

    if this sounds good can i start working on it .
    thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions