以下函数“increment”将1添加到表示为数组的数字中。
int* increment(int array[], int size, int *sizeLen) { int temp[size+1]; int carry = 0; carry = (array[size-1]+1)/10; temp[size] = (array[size-1]+1)%10; for(int i=size-2;i>=0;i--) { temp[i+1] = (array[i] + carry)%10; carry = (array[i]+carry)/10; } if(carry) { temp[0] = 1; *sizeLen = size+1; return temp; } else { *sizeLen = size; return (temp+1); } } int main() { int array[] = {9,9,9}; int length; int *res = increment(array, sizeof(array)/sizeof(int), &length); for(int i=0;i<length;i++) { cout << res[i] << " "; } }我知道gcc支持可变长度数组 ,它们存储在堆栈中。 一旦这个函数结束,我希望temp超出范围,并且在main中打印数组的尝试应该打印垃圾值。 但在我的情况下,打印实际值。 函数中声明的可变长度数组何时超出范围?
The following function "increment" add 1 to a number represented as an array.
int* increment(int array[], int size, int *sizeLen) { int temp[size+1]; int carry = 0; carry = (array[size-1]+1)/10; temp[size] = (array[size-1]+1)%10; for(int i=size-2;i>=0;i--) { temp[i+1] = (array[i] + carry)%10; carry = (array[i]+carry)/10; } if(carry) { temp[0] = 1; *sizeLen = size+1; return temp; } else { *sizeLen = size; return (temp+1); } } int main() { int array[] = {9,9,9}; int length; int *res = increment(array, sizeof(array)/sizeof(int), &length); for(int i=0;i<length;i++) { cout << res[i] << " "; } }I know gcc supports variable length arrays and they are stored on stack. I expect temp to go out of scope once this function ends and the attempt to print the array in main should print garbage values. But in my case the actual values are printed. When does the variable length array declared in the function goes out of scope?
最满意答案
这里的关键是数组的存储持续时间。 它具有自动存储持续时间。 具有自动存储持续时间的变量的生命周期在它们所在的范围退出时结束。
它完全按照您的预期进行。 但是C中没有任何内容阻止你获取本地对象的地址并从函数返回它。
使用该指针是未定义的行为。 它似乎可行,但阵列仍然是为了所有意图和目的“死”。 这种指针通俗地称为“悬挂指针”。
好吧,以上情况适用于C.因为这是关于GCC扩展,大致相同,但可能需要采取一些盐。
The key here is indeed the storage duration of the array. It has automatic storage duration. The lifetime of variables with automatic storage duration ends the moment the scope they're are in is exited.
It does exactly as you expect. But nothing in C stops you from taking the address of a local object and returning it from the function.
Using that pointer is undefined behavior. It may appear to work, but the array is still for all intents and purposes "dead". Such pointers are known colloquially as "dangling pointers".
Well, the above is true for C. Since this is about the GCC extension, the same applies mostly, but may need to be taken with a grain of salt.
从函数返回可变长度数组(Returning a variable length array from a function)以下函数“increment”将1添加到表示为数组的数字中。
int* increment(int array[], int size, int *sizeLen) { int temp[size+1]; int carry = 0; carry = (array[size-1]+1)/10; temp[size] = (array[size-1]+1)%10; for(int i=size-2;i>=0;i--) { temp[i+1] = (array[i] + carry)%10; carry = (array[i]+carry)/10; } if(carry) { temp[0] = 1; *sizeLen = size+1; return temp; } else { *sizeLen = size; return (temp+1); } } int main() { int array[] = {9,9,9}; int length; int *res = increment(array, sizeof(array)/sizeof(int), &length); for(int i=0;i<length;i++) { cout << res[i] << " "; } }我知道gcc支持可变长度数组 ,它们存储在堆栈中。 一旦这个函数结束,我希望temp超出范围,并且在main中打印数组的尝试应该打印垃圾值。 但在我的情况下,打印实际值。 函数中声明的可变长度数组何时超出范围?
The following function "increment" add 1 to a number represented as an array.
int* increment(int array[], int size, int *sizeLen) { int temp[size+1]; int carry = 0; carry = (array[size-1]+1)/10; temp[size] = (array[size-1]+1)%10; for(int i=size-2;i>=0;i--) { temp[i+1] = (array[i] + carry)%10; carry = (array[i]+carry)/10; } if(carry) { temp[0] = 1; *sizeLen = size+1; return temp; } else { *sizeLen = size; return (temp+1); } } int main() { int array[] = {9,9,9}; int length; int *res = increment(array, sizeof(array)/sizeof(int), &length); for(int i=0;i<length;i++) { cout << res[i] << " "; } }I know gcc supports variable length arrays and they are stored on stack. I expect temp to go out of scope once this function ends and the attempt to print the array in main should print garbage values. But in my case the actual values are printed. When does the variable length array declared in the function goes out of scope?
最满意答案
这里的关键是数组的存储持续时间。 它具有自动存储持续时间。 具有自动存储持续时间的变量的生命周期在它们所在的范围退出时结束。
它完全按照您的预期进行。 但是C中没有任何内容阻止你获取本地对象的地址并从函数返回它。
使用该指针是未定义的行为。 它似乎可行,但阵列仍然是为了所有意图和目的“死”。 这种指针通俗地称为“悬挂指针”。
好吧,以上情况适用于C.因为这是关于GCC扩展,大致相同,但可能需要采取一些盐。
The key here is indeed the storage duration of the array. It has automatic storage duration. The lifetime of variables with automatic storage duration ends the moment the scope they're are in is exited.
It does exactly as you expect. But nothing in C stops you from taking the address of a local object and returning it from the function.
Using that pointer is undefined behavior. It may appear to work, but the array is still for all intents and purposes "dead". Such pointers are known colloquially as "dangling pointers".
Well, the above is true for C. Since this is about the GCC extension, the same applies mostly, but may need to be taken with a grain of salt.
发布评论