[英]How is array passed to a function?
Consider the following: 考虑以下:
String[] array = {1,2,3,4};
myFunction(array);
public void myFunction(String[] array){
//some task here
}
I had to answer this question today. 我今天必须回答这个问题。
How are arrays passed to a function? 数组如何传递给函数? Means what is the underlying technique?
意味着什么是底层技术?
When I failed to answer, I was told the following. 当我没有回答时,被告知以下内容。
The address of first element is passed and other consecutive elements are obtained from the first element's address by adding some x bytes . 传递第一个元素的地址,并通过添加一些x个字节从第一个元素的地址获得其他连续元素。
Does this happen in every programming language or in just c and c++? 这会在每种编程语言中发生还是在c和c ++中发生?
Thank you! 谢谢!
No, in Java for example arrays are objects. 不,例如在Java中,数组是对象。 They are passed like any other object is passed to a method: the method would take a reference to the array object as a whole, and not a reference to the first element.
它们的传递就像将任何其他对象传递给方法一样:该方法将对整个数组对象进行引用,而不是对第一个元素的引用。
Taken from this page : 取自此页面 :
All class and array types inherit (§8.4.8) the methods of class Object
所有类和数组类型都继承(第8.4.8节)Object类的方法
Java has no concept of "pointers", in the same sense as C or C++ (addresses in memory), ie an object reference does not really point to the memory location where the object is stored. Java没有与C或C ++(内存中的地址)相同的“指针”概念,即,对象引用实际上并未指向存储对象的存储位置。
In theory, every language is different. 从理论上讲,每种语言都是不同的。 However:
然而:
In C, a function cannot take an array as an argument. 在C语言中,函数不能将数组作为参数。 When you declare an array parameter, the type is automatically converted into a pointer, so there is no different between
void f( int a[5] )
and void f( int* )
. 声明数组参数时,类型将自动转换为指针,因此
void f( int a[5] )
和void f( int* )
之间没有区别。 (This is often summarized by saying that arrays are not first class objects.) For reasons of C compatibility, C++ follows the same rules, but in C++, you wouldn't normally pass an array as a parameter anyway, and if you did, you would pass it by reference, where this conversion to pointer doesn't occur. (通常通过说数组不是一流的对象来概括这一点。)出于C兼容性的考虑,C ++遵循相同的规则,但是在C ++中,您通常不会将数组作为参数传递,如果这样做,您将通过引用传递它,在这种情况下不会发生指针转换。 (Ie
void f( int (&a)[5] )
is not the same as void f( int* &a )
.) (即
void f( int (&a)[5] )
是不一样的void f( int* &a )
In Java, and a number of other recent languages, everything (or almost), including arrays are objects, and parameters, variables, etc. are pointers to those objects. 在Java和许多其他最近的语言中,所有(或几乎所有),包括数组都是对象,而参数,变量等是指向这些对象的指针。 So in Java, you pass a pointer to the array, but the full array object, with all of the information about its size, etc. Sort of like passing an
std::vector<int>*
in C++. 因此,在Java中,您传递指向数组的指针,但传递完整的数组对象,并包含有关其大小的所有信息,等等。有点像在C ++中传递
std::vector<int>*
。
In a lot of languages (mostly older?), like Pascal and languages of the Modula family, and array is an object type just like any other. 在许多语言(大多是较旧的语言)中,例如Pascal和Modula家族的语言,数组和其他类型一样都是对象类型。 If you don't take any particular actions, an array will be passed by value, with a complete copy of the array.
如果您不执行任何特定操作,则将按值传递数组,并带有数组的完整副本。
And in the earliest languages, like Fortran or Algol, each language often had its own very particular ways of passing arrays, although in general, they followed the same rules as other types. 在最早的语言(如Fortran或Algol)中,每种语言通常都有其非常特殊的传递数组的方式,尽管总的来说,它们遵循与其他类型相同的规则。 (Some early languages like Cobol or Basic, didn't even support passing arguments to functions, at least in their earliest variants.)
(一些早期的语言,例如Cobol或Basic,甚至在最早的变体中甚至都不支持将参数传递给函数。)
Amongst the languages you're likely to see today, I think that the Java model predominates. 在您今天可能会看到的语言中,我认为Java模型占主导地位。 C remains an outlier, and C++ gives you the choice: you can pass an
std::vector
by value or by reference (but reference is recommended for performance reasons). C仍然是一个离群值,而C ++为您提供了选择:您可以按值或按引用传递
std::vector
(但出于性能考虑,建议使用引用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.