Values added to a vector with `push_back` cannot be used immediately
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start by reproducing the reprex and then inspect the writable::integers push_back() and element-access behavior in the cpp11 source. Compare the value read immediately after push_back() with the returned vector; done means immediate reads return the values just added, with a regression test covering the behavior.
Written by the indexing model from the issue text.
Description
I'm using a writable::integers to store results in an iterative process, where I want to use elements from the vector of integers in the process and update them. However, when I push_back() a value to the vector, and then immediately check its value I will get some random number. Weirdly, when the vector is returned to R it seems to look fine. It is illustrated in the reprex below where I print the value that I just pushed back to the vector.
Am I doing something wrong?
library(cpp11)
#> Warning: package 'cpp11' was built under R version 4.3.3
cpp_function("
writable::integers test() {
writable::integers x((R_xlen_t)0);
for (int i = 0; i < 10; i++) {
x.push_back(i);
Rprintf(\"When I want to us the integer x[i], I get %i instead of %i\\n\", x[i], i);
}
return x;
}
")
test()
#> When I want to us the integer x[i], I get -1107622224 instead of 0
#> When I want to us the integer x[i], I get -1107622224 instead of 1
#> When I want to us the integer x[i], I get -1107622224 instead of 2
#> When I want to us the integer x[i], I get -1107622224 instead of 3
#> When I want to us the integer x[i], I get -1107622224 instead of 4
#> When I want to us the integer x[i], I get -1107622224 instead of 5
#> When I want to us the integer x[i], I get -1107622224 instead of 6
#> When I want to us the integer x[i], I get -1107622224 instead of 7
#> When I want to us the integer x[i], I get -1107622224 instead of 8
#> When I want to us the integer x[i], I get -1107622224 instead of 9
#> [1] 0 1 2 3 4 5 6 7 8 9
Created on 2025-04-26 with reprex v2.0.2
- Dominant language
- C++
- Stars
- 224
- Forks
- 52
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from r-lib/cpp11
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
Similar issues
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 88/100
QuantStack/git2cpp#187 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
mlcommons/mobile_app_open#1182 ·
-
Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-cli#6547 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·