Board index » delphi » HELP: Problem with arrays in classes....

HELP: Problem with arrays in classes....

Sorry for asking such an obviously simple question, but I am new to Object
Pascal....

I am having trouble using an array in a class (in Delphi 4.0).  An array (the
error occurs whether the array is static or dynamic) is defined in a class
(Neuron.PAS), and instanciated in NeuralNetP.PAS.  Whenever I try to access the
array, I get the following error:

Project NEURALNET.EXE raised exception class EAccessViolation with message
'Access Violation at address 0043c975 in module 'NEURALNET.EXE'. Read of address
FFFFFFFF.' Process stopped.  Use Step or Run to continue.

The offending line is clearly marked in Neuron.PAS.  This is obviously a problem
with a missing pointer, but I am at a loss on how to correct it.  Any help would
be appriciated (especially because I need to figure this out in order to
complete my Master's thesis).

Please reply by E-mail, if at all possible:  mailto:ke...@mil.ufl.edu

-----------------------------------------------------
************** Beginning of NeuralNetP.PAS **************
-----------------------------------------------------
unit NuralNetP;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   StdCtrls, Neuron;

type
   TForm1 = class(TForm)
      Label1: TLabel;
      procedure FormCreate(Sender: TObject);
   private
      { Private declarations }
   public
      { Public declarations }
      MyNeuron  : TNeuron;
   end;

var
   Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
   i : Integer;
   s, ts : String;
begin
   with MyNeuron do begin
      for  i := 0 to 9 do begin
         Str(MyNeuron.Weight(i), ts);
         s := s + ', ' + ts;
      end;
      Label1.Caption := s;
   end;
end;

end.
-----------------------------------------------------
************** Beginning of Neuron.PAS **************
-----------------------------------------------------
unit Neuron;
interface
type
   TNeuron = class
   public
      { Public declarations }
      W : Array[0..9] of Single;
      Function Weight( Index : Integer ) : Single;
      Constructor Create( );
   end;
implementation

{ TNeuron }
constructor TNeuron.Create( );
var
   i : Integer;
begin
   for i := 0 to 9 do begin
      W[i] := i;
   end;
end;

function TNeuron.Weight(Index: Integer): Single;
begin
   Result := W[Index];     {*********** ERROR OCCURS THIS LINE ***********}
end;

end.

+--------------------------------------------------------------------+
|                     From - Kevin Harrelson                         |
| http://www.mil.ufl.edu/people/kevin/kevin.htm      Cool robots at: |
| mailto:ke...@mil.ufl.edu                    http://www.mil.ufl.edu |
+--------------------------------------------------------------------+
| "If I believe in God and life after death and you do not, and if   |
| there is no God, we both lose when we die. However, if there is a  |
| God, you still lose and I gain everything."       -- Blaise Pascal |
+--------------------------------------------------------------------+

 

Re:HELP: Problem with arrays in classes....


That error is windows complaining of an invalid memory access attempt.  You
are not properly initializing the pointer to your array or part of your
program does not correctly "see" this structure in memory.

Remember- In object Pascal you MUST typecast your pointers and pass them
Explicitly between far calls.  This may have been corrected in Delphi 4 but
I found this issue in serveral things I was trying in Delphi 3.

I would have to look at the code to help you more but track what pointer is
being passed, then track what you have assigned to that pointer.  Remember
you MUST have something like this in the code:

MyPointerofType =: TPointerType.Create;

or

MyPointerofType :=AnotherPointerofTypeThatHasBeenCreated;

You cannot implictly reference pointers because Object Pascal has strong
typecasting (Which I feel is a Very Good Thing).

Quote
Kevin Harrelson wrote in message <361E23FA.41ABC...@mil.ufl.edu>...
>Sorry for asking such an obviously simple question, but I am new to Object
>Pascal....

>I am having trouble using an array in a class (in Delphi 4.0).  An array
(the
>error occurs whether the array is static or dynamic) is defined in a class
>(Neuron.PAS), and instanciated in NeuralNetP.PAS.  Whenever I try to access
the
>array, I get the following error:

>Project NEURALNET.EXE raised exception class EAccessViolation with message
>'Access Violation at address 0043c975 in module 'NEURALNET.EXE'. Read of
address
>FFFFFFFF.' Process stopped.  Use Step or Run to continue.

>The offending line is clearly marked in Neuron.PAS.  This is obviously a
problem
>with a missing pointer, but I am at a loss on how to correct it.  Any help
would
>be appriciated (especially because I need to figure this out in order to
>complete my Master's thesis).

>Please reply by E-mail, if at all possible:  mailto:ke...@mil.ufl.edu

>-----------------------------------------------------
>************** Beginning of NeuralNetP.PAS **************
>-----------------------------------------------------
>unit NuralNetP;

>interface

>uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
>   StdCtrls, Neuron;

>type
>   TForm1 = class(TForm)
>      Label1: TLabel;
>      procedure FormCreate(Sender: TObject);
>   private
>      { Private declarations }
>   public
>      { Public declarations }
>      MyNeuron  : TNeuron;
>   end;

>var
>   Form1: TForm1;

>implementation

>{$R *.DFM}

>procedure TForm1.FormCreate(Sender: TObject);
>var
>   i : Integer;
>   s, ts : String;
>begin
>   with MyNeuron do begin
>      for  i := 0 to 9 do begin
>         Str(MyNeuron.Weight(i), ts);
>         s := s + ', ' + ts;
>      end;
>      Label1.Caption := s;
>   end;
>end;

>end.
>-----------------------------------------------------
>************** Beginning of Neuron.PAS **************
>-----------------------------------------------------
>unit Neuron;
>interface
>type
>   TNeuron = class
>   public
>      { Public declarations }
>      W : Array[0..9] of Single;
>      Function Weight( Index : Integer ) : Single;
>      Constructor Create( );
>   end;
>implementation

>{ TNeuron }
>constructor TNeuron.Create( );
>var
>   i : Integer;
>begin
>   for i := 0 to 9 do begin
>      W[i] := i;
>   end;
>end;

>function TNeuron.Weight(Index: Integer): Single;
>begin
>   Result := W[Index];     {*********** ERROR OCCURS THIS LINE ***********}
>end;

>end.

>+--------------------------------------------------------------------+
>|                     From - Kevin Harrelson                         |
>| http://www.mil.ufl.edu/people/kevin/kevin.htm      Cool robots at: |
>| mailto:ke...@mil.ufl.edu                    http://www.mil.ufl.edu |
>+--------------------------------------------------------------------+
>| "If I believe in God and life after death and you do not, and if   |
>| there is no God, we both lose when we die. However, if there is a  |
>| God, you still lose and I gain everything."       -- Blaise Pascal |
>+--------------------------------------------------------------------+

Other Threads