动态数组能否在非声明单元中分配空间?我这儿老师出错。请教。。 ( 积分: 50 )

  • 主题发起人 主题发起人 microlj
  • 开始时间 开始时间
M

microlj

Unregistered / Unconfirmed
GUEST, unregistred user!
动态数组在一个单元中只声明,不分配空间,在调用其他单元的函数中传递该数组,问这个其他单元中能否为该数组分配空间,如果能,应该如何分配呢?其原理能否说说。。。
比如unitA中声明 t:array of integer;在该单元中引用个unitB的函数link(t);
unitB中的函数为link(var s:array of integer);在unitB中分配空间 setlength(s,10)时 出错:不兼容类型。。
 
动态数组在一个单元中只声明,不分配空间,在调用其他单元的函数中传递该数组,问这个其他单元中能否为该数组分配空间,如果能,应该如何分配呢?其原理能否说说。。。
比如unitA中声明 t:array of integer;在该单元中引用个unitB的函数link(t);
unitB中的函数为link(var s:array of integer);在unitB中分配空间 setlength(s,10)时 出错:不兼容类型。。
 
你这个问题 跟你 的标题没有关系哦,

动态数组能否在非声明单元中分配空间 能!

你的出错并不是 上面的问题引起的。

这样声明就可以啦
type

TProcArray = array of integer;

procedure Proc(var s: TProcArray);
 
谢谢楼上的朋友,这个可以。。
不过为什么非得这样声明,能否做个解释。。
而且我调用这个函数的动态数组也得这样声明才可以。。。
 
link(var s:array of integer);函数形参不用分配空间的
在函数内直接访问
var i:Integer
begin
for i:=low(s) to High(s) do
begin
s:=i;
end;
end;

调用函数
var t:Array of Integer;
begin
Setlength(t,10);
link(t);
...
end;
 
我是想在调用过程中动态分配空间,不是在调用前定好其大小。。
 
unitB中的函数为link(var s:array of integer);
你这样声明参数 编译器 不认为 S 是 动态数组, 而是 Open array parameters(delphi帮助里面这样写的),不是同一类型。
你也可以看看Delphi 的帮助。

Open array parameters allow arrays of different sizes to be passed to the same procedure or function. To define a routine with an open array parameter, use the syntax array of type (rather than array[X..Y] of type) in the parameter declaration. For example,

function Find(A: array of Char): Integer;

declares a function called Find that takes a character array of any size and returns an integer.

Note

The syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thing. The previous example creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. To declare parameters that must be dynamic arrays, you need to specify a type identifier:

type TDynamicCharArray = array of Char;
function Find(A: TDynamicCharArray): Integer;

For information about dynamic arrays, see Dynamic arrays.

Within the body of a routine, open array parameters are governed by the following rules.

They are always zero-based. The first element is 0, the second element is 1, and so forth. The standard Low and High functions return 0 and Length - 1, respectively. The SizeOf function returns the size of the actual array passed to the routine.
They can be accessed by element only. Assignments to an entire open array parameter are not allowed.
They can be passed to other procedures and functions only as open array parameters or untyped var parameters. They cannot be passed to SetLength.

Instead of an array, you can pass a variable of the open array parameter's base type. It will be treated as an array of length 1.

When you pass an array as an open array value parameter, the compiler creates a local copy of the array within the routine's stack frame. Be careful not to overflow the stack by passing large arrays.

The following examples use open array parameters to define a Clear procedure that assigns zero to each element in an array of reals and a Sum function that computes the sum of the elements in an array of reals.

procedure Clear(var A: array of Real);
var
I: Integer;
begin
for I := 0 to High(A) do A := 0;
end;
function Sum(const A: array of Real): Real;
var
I: Integer;
S: Real;
begin
S := 0;
for I := 0 to High(A) do S := S + A;
Sum := S;
end;

When you call routines that use open array parameters, you can pass open array constructors to them.

Variant open array parameters
 
感谢xuxiaohan回答,也感谢Supermay的参与。。。
 
后退
顶部