传递指针然后分配可变长度数组入栈
Passing pointer and then allocating variable length array to stack
是否可以在一个函数中从另一个函数向堆栈分配可变长度数组?
一种可行的方法是预先分配最大可能的大小,但我想知道是否有办法避免这种情况。
void outside_function(){
char[] place_to_allocate_stack_array;
size_t array_size = allocate_and_fill_array(place_to_allocate_stack_array);
//do stuff with the now allocated variable length array on stack
}
size_t allocate_and_fill_array(char* place_to_allocate){
//does some stuff to determine how long the array needs to be
size_t length= determine_length();
//here I want to allocate the variable length array to the stack,
//but I want the outside_function to still be able to access it after
//the code exits allocate_and_fill_array
place_to_allocate[length];
//do stuff to fill the array with data
return length;
}
size_t determine_length(){
////unknown calculations to determine required length
}
不,甚至忽略人们对使用可变长度数组 (VLA) 的担忧。您试图在一个函数中完成太多。退后一步,看看你在问什么。
为了保持一致性并摆脱数组,我将重命名一些东西。考虑这个版本的设置:
class X; // instead of "char []" so we can drop the VLA baggage
size_t inner_function(X & data) { // was "allocate_and_fill_array"
// Determine how data should be allocated
// Do stuff with data
}
void outer_function() {
X data;
size_t data_size = inner_function(data);
}
要求 #1: 内部函数需要访问在外部函数中声明的变量。这需要将变量作为参数传递给内部函数。这又要求在声明变量后调用内部函数。
要求 #2: 内部函数决定了 data
应该如何分配(发生在声明点)。这就要求在声明变量之前调用内部函数。
这些要求具有相互矛盾的先决条件。不可能。
我被引出了一个问题:是什么让您采用这种方法?您已经编写了一个单独的 determine_length
函数。让 outside_function
调用它,声明 VLA,然后将 VLA 和长度传递给内部函数。概念上更简单。
size_t determine_length() {
// unknown calculations to determine required length
}
void fill_array(char* stack_array, size_t length) {
//do stuff to fill the array with data
}
void outside_function(){
size_t length = determine_length();
char stack_array[length];
fill_array(stack_array, length);
//do stuff with the variable length array on stack
}
不过,这种对获取堆栈数据的痴迷可能是premature。虽然堆存储确实比堆栈存储更昂贵,但差异通常不值得担心。让你的程序运行起来,然后再通过篮球来调整性能。专注于稳健性。只有在分析器识别出性能瓶颈后才花时间解决它。
是否可以在一个函数中从另一个函数向堆栈分配可变长度数组?
一种可行的方法是预先分配最大可能的大小,但我想知道是否有办法避免这种情况。
void outside_function(){
char[] place_to_allocate_stack_array;
size_t array_size = allocate_and_fill_array(place_to_allocate_stack_array);
//do stuff with the now allocated variable length array on stack
}
size_t allocate_and_fill_array(char* place_to_allocate){
//does some stuff to determine how long the array needs to be
size_t length= determine_length();
//here I want to allocate the variable length array to the stack,
//but I want the outside_function to still be able to access it after
//the code exits allocate_and_fill_array
place_to_allocate[length];
//do stuff to fill the array with data
return length;
}
size_t determine_length(){
////unknown calculations to determine required length
}
不,甚至忽略人们对使用可变长度数组 (VLA) 的担忧。您试图在一个函数中完成太多。退后一步,看看你在问什么。
为了保持一致性并摆脱数组,我将重命名一些东西。考虑这个版本的设置:
class X; // instead of "char []" so we can drop the VLA baggage
size_t inner_function(X & data) { // was "allocate_and_fill_array"
// Determine how data should be allocated
// Do stuff with data
}
void outer_function() {
X data;
size_t data_size = inner_function(data);
}
要求 #1: 内部函数需要访问在外部函数中声明的变量。这需要将变量作为参数传递给内部函数。这又要求在声明变量后调用内部函数。
要求 #2: 内部函数决定了 data
应该如何分配(发生在声明点)。这就要求在声明变量之前调用内部函数。
这些要求具有相互矛盾的先决条件。不可能。
我被引出了一个问题:是什么让您采用这种方法?您已经编写了一个单独的 determine_length
函数。让 outside_function
调用它,声明 VLA,然后将 VLA 和长度传递给内部函数。概念上更简单。
size_t determine_length() {
// unknown calculations to determine required length
}
void fill_array(char* stack_array, size_t length) {
//do stuff to fill the array with data
}
void outside_function(){
size_t length = determine_length();
char stack_array[length];
fill_array(stack_array, length);
//do stuff with the variable length array on stack
}
不过,这种对获取堆栈数据的痴迷可能是premature。虽然堆存储确实比堆栈存储更昂贵,但差异通常不值得担心。让你的程序运行起来,然后再通过篮球来调整性能。专注于稳健性。只有在分析器识别出性能瓶颈后才花时间解决它。