为什么不能在C中返回固定大小/ const数组? [英] Why you cannot return fixed size / const array in C?

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

问题描述

我想知道为什么无法在C中返回数组?
毕竟,数组只是由大小信息支持的指针(使 sizeof 起作用)。首先,我认为这样做是为了防止我返回堆栈中定义的数组,但是没有什么阻止我返回指向堆栈中某些内容的指针(gcc警告我,但代码可以编译)。而且我还可以返回字符串文字,它是字符的静态存储数组。顺便说一句,在lunux中,它存储在 .rodata 中,并且const数组也存储在这里(用 objdump ),所以我可以返回数组(将其广播到指针)并且可以工作,但是AFAIK只是特定于实现的(另一个OS / Compiler可以将const存储在堆栈上)。

I wonder why it is not possible to return array in C? After all, array is just a pointer backed by size info (to make sizeof work). First I thought this was done to prevent me from returning array defined on my stack, but nothing prevents me from returning pointer to something on my stack (gcc warns me, but code compiles). And I also can return string literal which is statically storaged array of chars. By the way, in lunux it is stored in .rodata, and const array is stored there also (check it with objdump), so I can return array (casting it to pointer) and it works, but AFAIK this is just implementation-specific (another OS/Compiler may store const on stack).

我有两个想法如何实现数组返回:只需将其复制为值(对结构完成即可。我什至可以返回将其包装到结构中的数组!),并自动创建指向它的指针或允许用户返回const数组并创建协定,使该数组应具有静态存储持续时间(就像对字符串所做的那样)。这两个想法都是微不足道的!
所以,我的问题是为什么K& R没有实现类似的东西?

I have 2 ideas how to implement array returning: Just copy it as value (as it is done for structure. I even can return array wrapping it into structure!!), and create pointer to it automatically or allow user to return const array and create contract that such array should have static storage duration (as it done for strings). Both ideas are trivial! So, my question is why K&R did not implement something like that?

推荐答案

数组不是只是一个由尺寸信息支持的指针。

An array is not "just a pointer backed by size info".

数组是某种类型的连续元素的块。没有指针。

An array is a block of contiguous elements of a certain type. There is no pointer.

由于数组是一个对象,因此可以形成一个指向数组或数组元素之一的指针。但是这样的指针不是数组的一部分,也不与数组一起存储。说一个 int 只是一个由 1 int大小支持的指针就有意义。

Since an array is an object, a pointer can be formed which points to the array, or to one of the array's elements. But such a pointer is not part of the array and is not stored with the array. It would make as much sense to say "an int is just a pointer backed by a size of 1 int".

编译器知道数组的大小的方法与知道任何对象的大小的方法相同。如果我们有 double d; ,那么已知 sizeof d sizeof(double) ,因为编译器记得 d double 类型的对象。

The size of an array is known by the compiler in the same way that the size of any object is known. If we have double d; then it is known that sizeof d is sizeof(double) because the compiler remembers that d is an object of type double.


没有什么可以阻止我返回指向堆栈中某物的指针

nothing prevents me from returning pointer to something on my stack

C标准阻止您执行此操作(并使用返回的指针)。如果您编写的代码违反了标准,那么您就只能靠自己了。

The C standard prevents you from doing this (and using the returned pointer). If you write code that violates the standard then you are on your own.


我也可以返回字符串文字

And I also can return string literal

字符串文字是char的数组。在return语句中使用数组时,该数组将转换为指向第一个元素的指针。

A string literal is an array of char. When you use an array in a return statement, it is converted to a pointer to the first element.

要使数组能够按值返回(并赋值),有关将数组转换为指针的规则(有时称为衰减)必须更改。这是可能的,但是K& R决定在设计C时使衰减几乎无处不在。

To enable arrays to be returned (and assigned) by value, the rule regarding conversion of array to pointer (sometimes called "decay") would have to be changed. This would be possible, but K&R decided to make the decay almost ubiquitous when designing C.

实际上,可能有像C这样的语言,但无需完全衰减。事后看来,这可能会避免很多混乱。但是,他们只是选择以自己的方式实施C。

In fact it would be possible to have a language like C but without having the decay at all. Maybe in hindsight that would have saved a lot of confusion. However they just chose to implement C in the way that they did.

在K& RC中,不可能要么按值返回结构。任何不是原始类型的复制操作,都必须使用 memcpy 或等效的迭代副本来完成。考虑到1970年代硬件资源的使用方式,这似乎是一个合理的设计决策。

In K&R C, it was not possible to return structures by value either. Any copy operation that was not a primitive type, had to be done with memcpy or an equivalent iterative copy. This seems like a reasonable design decision given the way hardware resources were in the 1970s.

ANSI C增加了按值返回结构的可能性,但是到那时,即使他们想改变,也来不及改变衰减规则;它将破坏很多依赖衰减规则的现有代码。

ANSI C added the possibility to return structures by value , however by then it would have been too late to change the decay rule even if they had wanted to; it would break a lot of existing code which is relying on the decay rule.

这篇关于为什么不能在C中返回固定大小/ const数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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