函数如何返回数组指针。

wo_315 2004-11-18 12:29:46
我的程序如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

TBpnn = class
// 成员变量

// 输入层神经元个数 int input_n;
Input_n : integer;

// 隐含层神经元个数 int hidden_n;
Hidden_n : integer;

// 输出层神经元个数 int output_n;
Output_n : Integer;

// 输入层神经元 double *input_units;
Intput_units : double; //这里改写成Input_units : array of
//double;那么Input_units就是指向动态
//数组地址的指针。那么对应的函数应该
//如何写他的返回指呢,返回的也是指针。


// 隐含层神经元 double *hidden_units;
Hidden_units : double;

// 输出层神经元 double *output_units;
Output_units : double;

// 隐含层误差 double *hidden_delta;
Hidden_delta : double;

// 输出层误差 double *output_delta;
Output_delta : double;

// 训练目标值 double *target;
Targer : double;

// 输入层权值矩阵 double **input_weights;
Input_weights : double;

// 隐含层权值矩阵 double **hidden_weights;
Hidden_weights: double;

// 前输入层权值矩阵 double **input_prev_weights;
Input_prev_weights: double;

// 前隐含层权值矩阵 double **hidden_prev_weights;
Hidden_prev_weights: double;


private

// 返回 0-1 的双精度随机数 double drnd(void);函数
function Drnd(x:integer): double;

// 返回-1.0 - 1.0之间的双精度随机数 double dpn1(void);函数
function Dpn1(x:integer): double;

// 作用函数,这里使用 S 函数 double squash(double x);
function Squash(x: double): double;

// 申请1位双精度实数数组 double * alloc_1d_dbl(int n); 函数
function alloc_1d_dbl(n: integer): double;

// 申请2位双精度实数数组 double ** alloc_2d_dbl(int m, int n); 函数
function alloc_2d_dbl(m,n: integer): double;

// 随机初始化权值 void bpnn_randomize_weights(double ** w, int m, int n);过程
procedure Bpnn_randomize_weights(m,n: integer);

// 0值化权值 void bpnn_zero_weights(double ** w, int m , int n); 过程
procedure Bpnn_zero_weights(m,n: integer);

// 创建神经网络 void bpnn_internal_create(int n_in, int n_hidden, int n_out);过程
procedure Bpnn_internal_create(n_in,n_hidden,n_out: integer);

// 输出称误差
procedure Bpnn_output_error(delta: double;target: double;output: double;nj:
integer;err: double);

// 隐含层误差
procedure Bpnn_hidden_error(delta: double;nh: integer;delta_o: double;no:
integer;who:array of double;hidden: double; err: double);

// 神经网络权值调整
procedure Bpnn_adjust_weights(delta: double;ndelta: integer;ly: double; nly:
integer;w: array of double;oldw: array of double;momentum: double);

// 单层神经网络前向运动
procedure Bpnn_layerforward(l1: double;l2: double;Conn: array of integer;n1: integer;n2: integer);

// 创建神经网络 void bpnn_create(int n_in, int n_hidden, int n_out);
procedure Bpnn_create(n_in,n_hidden,n_out: integer);

// 训练BP神经网络
procedure Bpnn_train(eta, momentum: double;eo,eh: array of double);

// 神经元前向运算 void bpnn_feedforward(void);
procedure Bpnn_feedforward();

public


// 传送输入参数给神经网络
procedure Bpnn_getelement(in_units: double;ta_units: double);

//加入输入单元 void bpnn_SetInputElement(double * in_units);过程
procedure Bpnn_SetInputElement(in_units: double);

// 取出输出单元 void bpnn_GetOutputElement(double * out_units);过程
procedure Bpnn_GetOutPutElement(out_units: double);
end;


var
Form1: TForm1;
Bp : Tbpnn;

implementation

{$R *.dfm}

//返回0~1的双精度随机数
function Tbpnn.Drnd(x:integer): double;
begin
Result := random(x);
end;

//返回-1~1的双精度随机数
function Tbpnn.Dpn1(x:integer): double;
begin
Result := Drnd(x)*2.0-1.0;
end;

//作用函数,这里选用的是S型函数
//参数X是自变量
function Tbpnn.Squash(x: double): double;
begin
Result := 1.0/(1.0+exp(-X));
end;

//申请1维的双精度实数数组
//参数:n为数组的元数个数;
function Tbpnn.alloc_1d_dbl(n: integer): double;
var
New1 : array of double;
begin
SetLength(New1,n);
if New1 = nil then
Result := 0
else
Result := New1[n]; //这个地方我如果我改成Result := New1;就是错误。而函数function
//Tbpnn.alloc_1d_dbl(n: integer): double;我如何让他返回的是个指针
//也就是一维数组的地址。
end;

//申请2维的双精度实数数组
//参数:m为数组的行数
// n为数组的列数
function Tbpnn.alloc_2d_dbl(m,n: integer):double;
var
New1 : array of array of double;
I : integer;
begin
SetLength(New1,m,n);
if New1 = nil then
Result := 0
else
for i:= 0 to m do
New1[i,n] := alloc_1d_dbl(n);
Result := New1[m,n];
end;

//随机初始化权值
//参数:w为保存权值的2维数组
// m为数组的行数
// n为数组的列数
procedure Tbpnn.Bpnn_randomize_weights(m,n: integer);
var
w: array of array of double;
i,j,x : integer;
begin
Setlength(w,m,n);
for i:= 0 to m do
for j:= 0 to n do
w[i,j] := Dpn1(x);
end;

procedure Tbpnn.Bpnn_zero_weights(m,n: integer);
var
w : array of array of double;
i,j : integer;
begin
for i:= 0 to m do
for j:= 0 to n do
w[i,j] := 0.0;
end;

//创建BPNN网络
//参数:n_in为输入层神经元个数
// n_hidden为隐含层神经元个数
// n_out为输入层神经元个数
procedure Tbpnn.Bpnn_internal_create(n_in,n_hidden,n_out: integer);
begin
Bp.Input_n := n_in;
Bp.Hidden_n := n_hidden;
Bp.Output_n := n_out;

Bp.Intput_units := alloc_1d_dbl(n_in+1);
Bp.Hidden_units := alloc_1d_dbl(n_hidden+1);
Bp.Output_units := alloc_1d_dbl(n_out+1);

Bp.Hidden_delta := alloc_1d_dbl(n_hidden+1);
Bp.Output_delta := alloc_1d_dbl(n_out+1);
Bp.Targer := alloc_1d_dbl(n_out+1);

Bp.Input_weights := alloc_2d_dbl(n_in+1,n_hidden+1);
Bp.Hidden_weights := alloc_2d_dbl(n_hidden+1,n_out+1);

Bp.Input_prev_weights := alloc_2d_dbl(n_in+1,n_hidden+1);
Bp.Hidden_prev_weights:= alloc_2d_dbl(n_hidden+1,n_out+1);

end;

procedure Bpnn_feedforward();
begin

end;


procedure Tbpnn.Bpnn_output_error(delta: double;target: double;output: double;nj: integer;err: double);
begin

end;

procedure Tbpnn.Bpnn_hidden_error(delta: double;nh: integer;delta_o: double;
no: integer;who: array of double;hidden: double; err: double);
begin

end;

procedure Tbpnn.Bpnn_adjust_weights(delta: double;ndelta: integer;ly: double; nly: integer;w: array of double;oldw: array of double;momentum: double);
begin

end;

procedure Tbpnn.Bpnn_train(eta, momentum: double;eo,eh: array of double);
begin

end;

procedure Tbpnn.Bpnn_feedforward();
begin
end;

procedure Tbpnn.Bpnn_layerforward(l1: double;l2: double;Conn: array of integer;n1: integer;n2: integer);
begin
end;

procedure Tbpnn.Bpnn_create(n_in,n_hidden,n_out: integer);
begin

end;

procedure Tbpnn.Bpnn_getelement(in_units: double;ta_units: double);
begin

end;

procedure Tbpnn.Bpnn_SetInputElement(in_units: double);
begin

end;

procedure Tbpnn.Bpnn_GetOutPutElement(out_units: double);
begin

end;


end.

动态数组定义了之后,例如:Input_units : array of double;实际上这个Input_units指向的是动态数组的地址。那么在类的实例Bp中,我调用Input_units,对应的函数alloc_1d_dbl()我如何让他返回值也是数组的地址。我上面为什么把Input_units定义为double,是因为类型不匹配我没有办法(为了让程序通过而做的,实际上是错的)。这里主要的就是让函数alloc_1d_dbl()能跟Input_units类型匹配。
大哥们可以教教我吗?其他的想我应该可以举一反三的。谢谢了。
...全文
927 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wo_315 2004-11-18
  • 打赏
  • 举报
回复
哦。谢谢楼上的各位了。我那个程序是C++的所以有点麻烦!
我把分给大家。
ICMGDCHN 2004-11-18
  • 打赏
  • 举报
回复
动态数组以数组名做为数组首地址.也就是数组指针.
halfdream 2004-11-18
  • 打赏
  • 举报
回复
type
doubleArray=array of double;//一定要这样定义后,编译器才会把它作一个类型来处理.
double2D_Array=array of doubleArray;

function alloc_1d_dbl(n: integer): doubleArray;
begin
SetLength(Result,n);
end;

function alloc_2d_dbl(m,n:integer):double2D_Array;
var
i:Integer;
begin
SetLength(Result,m);
for i:= to n-1 do
begin
SetLength(Result[i],n);
end;

end;
ICMGDCHN 2004-11-18
  • 打赏
  • 举报
回复
不好意思,上面那个
mov eax,esi应该是mov eax,ebp才对
「已注销」 2004-11-18
  • 打赏
  • 举报
回复
不可能将局部变量的地址返回的!
解决就是上面所说通过参数返回。
ICMGDCHN 2004-11-18
  • 打赏
  • 举报
回复
function getArrayAddr(n:integer):integer;
var
arr:array of integer;
begin
setLength(arr,n);
asm
mov eax,esi //返回数组首地址
end
end;
ICMGDCHN 2004-11-18
  • 打赏
  • 举报
回复
function getArrayAddr(n:integer):Integer;
var
jinjazz 2004-11-18
  • 打赏
  • 举报
回复
传参数
wo_315 2004-11-18
  • 打赏
  • 举报
回复
还是不清楚!!!
halfdream 2004-11-18
  • 打赏
  • 举报
回复
动态数组的内存是靠引用计数维护的,如果你仅仅是传个指针,会丢掉引用,内存会被释放的.
wo_315 2004-11-18
  • 打赏
  • 举报
回复
为什么没有人肯教教我!!!
555555555555555555

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧