我正在使用一個很好的GCC擴展,它允許我們在結構中聲明VLA。現在我發現了一種通過這種方式將VLA傳遞給函數的方法(通過值)。我也找到了一種方法來回報一個,但在一個非常有限的情況下。如何在每個函數實例上返回大小不同的VLA?
本實施例的功能碼是這樣的:
extern void func3()
{
size_t size;
scanf("%zu", &size);
struct tx{int _[size];} fn()
{
struct tx rt;
for(size_t i=0; i < size; ++i)
scanf("%d", &rt._[i]);
return rt;
}
volatile __typeof__(fn) *pf = fn;
}
上面的例子被設計用於測試目的(特別是比較編譯它的二進制代碼)。
但是,這是相當有限的,因爲函數的不同調用之間返回數組的大小不變。
如何使返回的數組大小等於函數參數之一或本函數中的某個其他本地參數。
我不認爲alloca
可以幫助我的情況下,因爲它分配的內存立即銷燬在函數出口(IRC)。
我想寫的東西是這樣的:
/*???*/ func5()
{
size_t size;
scanf("%zu", &size);
struct {int _[size];} rt;
for(size_t i=0; i < size; ++i)
scanf("%d", &rt._[i]);
return rt; //ok - return the structure
}
換句話說,這可能是問號裏面的類型?或者也許有其他解決方案(但不使用malloc
)?
這樣的功能的理論使用將理論上需要另一種類型的存儲返回值作爲返回的結構體的大小將不提供給調用者(除非有某種方式避免這種?)。但在第一眼看到它應該是這樣的:
size_t size;
//scanf("%zu", &size);
struct {int _[size];} tmp; //create locally VM type
//compatible with the one
//returned by our theoretical func5
//we can't directly initialize tmp here (gcc complains)
tmp = ((__typeof__(tmp) (*)())func5)(); //direct assignment between VM structures
//works here on the other hand
//as function return value is rvalue and we can't
//take its pointer and cast it to our local VM structure type
//we instead cast the function pointer
如果我們做這樣的事情:
__typeof__(func5()) tmp = func5();
它不會因爲虛擬機返回類型的func5
工作將要麼依賴於它的參數或局部變量。 但是,這一切都是理論上的,因爲我們仍然不能定義這個功能。
只是讓呼叫者第一分配陣列和被叫只是在填入值。 – user3528438
@ user3528438我對其他解決方案不感興趣。我希望將VLA分配保存在被調用的函數中(並且不使用'malloc')。 – AnArrayOfFunctions
我想看看這是如何編碼的。像'{type_TBD func5(); type_TBD y = func5(); //使用y}'?你能發表一些使用'func5()'結果的理論代碼嗎? – chux