为什么cant函数返回数组 [英] why cant functions return arrays

查看:73
本文介绍了为什么cant函数返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下声明在C中无效?

int f()[];

int f()[10];


如果有人能为这样的语言限制解释设计决定

,那将是很棒的。


问候,

Sanjay

Why are the following declarations invalid in C?

int f()[];
int f()[10];

It would be great if anyone could also explain the design decision
for such a language restricton.

Regards,
Sanjay

推荐答案

文章< fd **************** ******************@1g2000prg.go oglegroups.com>,
sa ************** @ gmail.com < sa ************** @ gmail.com写道:
In article <fd**********************************@1g2000prg.go oglegroups.com>,
sa**************@gmail.com <sa**************@gmail.comwrote:

>为什么以下声明在C中无效?
>Why are the following declarations invalid in C?


> int f()[];
int f()[10];
>int f()[];
int f()[10];


>如果有人也可以解释这种语言限制的设计决定,那将是很棒的。
>It would be great if anyone could also explain the design decision
for such a language restricton.



数组临时存储在哪里?谁将负责

用于释放它?


在基于堆栈的机器中混合参数和控制,

信息对于被调用的例程,在创建假设返回数组的时候已经在堆栈中。由于返回数组可以是任意大小的b $ b,因此来电者无法知道预留多少空间

在...之下被调用例程的堆栈帧。因此,所谓的

例程要么以某种方式插入数组的空间

" above例程本身的堆栈框架(因此当例程返回时它仍然存在
),否则被调用的例程将需要返回数组的地址。如果它返回一个数组的地址

,那么地址不能是被调用例程中的自动变量的地址,就像调用那些自动变量之后那样

无法访问。它不能使用静态变量,因为它不知道
知道最大尺寸。因此,实现这项工作的唯一方法是将malloc()数组存储到数据中,存储数据,以及

返回指向malloc的指针区域。但那么关于谁释放阵列的合同是什么?如果您需要调用例程

-automatically-释放数组,那么您将为语言添加显着的复杂性

。如果你需要调用例程 - 明确地 -

释放数组,那么你没有比你更具表现力的功能

已经有了返回类型指针和返回

指向malloc'区域的指针。

-

历史是一堆碎片 - Laurie Anderson

Where would the array get temporarily stored? Who would be responsible
for freeing it?

In a stack-based machine that mixes parameters and control, the
information for the called routine is already on the stack by the time
the hypothethical return array is created. As the return array could
be of any size, the caller cannot know how much space to reserve
"under" the stack frame for the called routine. Therefor the called
routine would either have to somehow insert the space for the array
"above" the stack frame for the routine itself (so that it still
exists when the routine returns), or else the called routine would
have to return an address of the array. If it returns an address of
the array, the address cannot be that of an automatic variable in
the called routine, as after the call those automatic variables
become inaccessible. It can''t use a static variable because it doesn''t
know the maximum size. So the only way to make that work would be
to malloc() an array to store the data into, store the data, and
return the pointer to the malloc''d area. But then what''s the contract
about who frees the array? If you require that the calling routine
-automatically- frees the array, then you add noticable complexity
to the language. If you require that the calling routine -explicitly-
free the array, then you have no more expressive power than you
already have available by making the return type a pointer and returning
the pointer to a malloc''d area.
--
"History is a pile of debris" -- Laurie Anderson


Walter Roberson写道:
Walter Roberson wrote:

文章< fd ****** ****************************@1g2000prg.go oglegroups.com>,
sa ************** @ gmail.com < sa ***** *********@gmail.com写道:
In article <fd**********************************@1g2000prg.go oglegroups.com>,
sa**************@gmail.com <sa**************@gmail.comwrote:

>为什么以下声明在C中无效?
>Why are the following declarations invalid in C?


> int f()[];
int f()[10];
>int f()[];
int f()[10];


>如果有人也可以解释这种语言限制的设计决定,那将是很棒的。
>It would be great if anyone could also explain the design decision
for such a language restricton.



阵列临时存储在哪里?


Where would the array get temporarily stored?



无论是在最终目的地还是在临时变量中,因为

都来自函数。

Either in its final destination or in a temporary variable, as
all results from functions.


谁将负责

用于解冻它?
Who would be responsible
for freeing it?



没有人,它可能是结果变量或在一个临时的自动存储类型的


Nobody, it would be either in a result variable or in a
temporary with automatic storage type.


在一个混合参数和控制的基于堆栈的机器中,

被调用例程的信息在创建假设返回数组时已经在堆栈中。由于返回数组可以是任意大小的b $ b,因此来电者无法知道预留多少空间

在...之下被调用例程的堆栈帧。因此,所谓的

例程要么以某种方式插入数组的空间

" above例程本身的堆栈框架(因此当例程返回时它仍然存在
),否则被调用的例程将需要返回数组的地址。如果它返回一个数组的地址

,那么地址不能是被调用例程中的自动变量的地址,就像调用那些自动变量之后那样

无法访问。它不能使用静态变量,因为它不知道
知道最大尺寸。因此,实现这项工作的唯一方法是将malloc()数组存储到数据中,存储数据,以及

返回指向malloc的指针区域。但那么关于谁释放阵列的合同是什么?如果您需要调用例程

-automatically-释放数组,那么您将为语言添加显着的复杂性

。如果你需要调用例程 - 明确地 -

释放数组,那么你没有比你更具表现力的功能

已经有了返回类型指针和返回

指向malloc''d区域的指针。
In a stack-based machine that mixes parameters and control, the
information for the called routine is already on the stack by the time
the hypothethical return array is created. As the return array could
be of any size, the caller cannot know how much space to reserve
"under" the stack frame for the called routine. Therefor the called
routine would either have to somehow insert the space for the array
"above" the stack frame for the routine itself (so that it still
exists when the routine returns), or else the called routine would
have to return an address of the array. If it returns an address of
the array, the address cannot be that of an automatic variable in
the called routine, as after the call those automatic variables
become inaccessible. It can''t use a static variable because it doesn''t
know the maximum size. So the only way to make that work would be
to malloc() an array to store the data into, store the data, and
return the pointer to the malloc''d area. But then what''s the contract
about who frees the array? If you require that the calling routine
-automatically- frees the array, then you add noticable complexity
to the language. If you require that the calling routine -explicitly-
free the array, then you have no more expressive power than you
already have available by making the return type a pointer and returning
the pointer to a malloc''d area.



然后,请告诉我为什么这样有效?


struct f {int array [80];};


struct f函数(无效)

{

struct f result;

返回结果;

}


这是一样的。


函数不能返回数组因为......任意

决定是在很久以前完成的,必须保留,以便

新软件与过去的

错误保持兼容。


-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
< a rel =nofollowhref =http://www.cs.virginia.edu/~lcc-win32target =_ blank> http://www.cs.virginia.edu/~lcc-win32


sa **** **********@gmail.com 写道:

为什么以下声明在C中无效?


int f()[];

int f()[10];


如果有人也可以解释这样一种语言的设计决定

会很棒限制。


问候,

Sanjay
Why are the following declarations invalid in C?

int f()[];
int f()[10];

It would be great if anyone could also explain the design decision
for such a language restricton.

Regards,
Sanjay



没有理由。这只是该语言的一个设计错误。

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs .virginia.edu / ~lcc-win32


这篇关于为什么cant函数返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆