Skip to content
Browse files

Use R vector instead of VLA.

Fix windows segfault?
  • Loading branch information...
1 parent 6e2d60e commit 03e1af8a76fda1cd6a798d8521ea1f1b0e404899 @hadley committed
Showing with 11 additions and 8 deletions.
  1. +11 −8 src/split-numeric.c
View
19 src/split-numeric.c
@@ -10,31 +10,34 @@ SEXP split_indices(SEXP group, SEXP n) {
int *pgroup = INTEGER(group);
// Count number of cases in each group
- int counts[nlevs];
+ SEXP counts;
+ PROTECT(counts = allocVector(INTSXP, nlevs));
+ int *pcounts = INTEGER(counts);
+
for (i = 0; i < nlevs; i++)
- counts[i] = 0;
+ pcounts[i] = 0;
for (i = 0; i < nobs; i++) {
j = pgroup[i];
if (j > nlevs) error("n smaller than largest index");
- counts[j - 1]++;
+ pcounts[j - 1]++;
}
// Allocate storage for results
PROTECT(vec = allocVector(VECSXP, nlevs));
for (i = 0; i < nlevs; i++) {
- SET_VECTOR_ELT(vec, i, allocVector(INTSXP, counts[i]));
+ SET_VECTOR_ELT(vec, i, allocVector(INTSXP, pcounts[i]));
}
// Put indices in groups
for (i = 0; i < nlevs; i++) {
- counts[i] = 0;
+ pcounts[i] = 0;
}
for (i = 0; i < nobs; i++) {
j = pgroup[i] - 1;
- k = counts[j];
+ k = pcounts[j];
INTEGER(VECTOR_ELT(vec, j))[k] = i + 1;
- counts[j]++;
+ pcounts[j]++;
}
- UNPROTECT(1);
+ UNPROTECT(2);
return vec;
}

0 comments on commit 03e1af8

Please sign in to comment.
Something went wrong with that request. Please try again.