The conflict arrises because both variable length arrays and alloca allocations are stack based: the stack is extended to make the required space, but variable length arrays are supposed to be limited to the block they are defined in which alloca allocations are supposed to persist until the subroutine they are made in is exited.
The sample code is:
{
char arr[strlen(s)];
fill_in(arr, s);
s = strdupa(arr);
}
When the block containing the variable length array definition is entered the stack is extended to make space for the array. Presumably (the article doesn't say) this allocation is removed from the stack when the block is exited (it could also be that it is removed when the block is re-entered and when the subroutine is exited). The conflict arises when alloca is used to allocate another block on the stack because the alloca allocation must persist to the end of the subroutine. As the alloca allocation is above the variable length array allocation on the stack, the variable length array allocation can't be freed until after the alloca allocation is freed and this can't happen before the subroutine is exited. A new allocation could be made for the variable length array on every entry to the block, but then these would accumulate, which isn't supposed to happen.
No comments:
Post a Comment