Board index » delphi » What's the difference? Memory Question.

What's the difference? Memory Question.

Hello all,  figured I would post a question for a change.

What is the difference between the two following code segments?

*****Example 1*****

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    I : Array[1..100] of Integer;                    {<---Question}
    MyString :  Array[1..100] of String;        {<---Question}
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

*****Example 2*****

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  I : Array[1..100] of Integer;                    {<---Question}
  MyString :  Array[1..100] of String;        {<---Question}

implementation

{$R *.DFM}

*******

I am aware that if I want to use the variables I[x] and MyString[x] in
another unit from the first example then I would have to put Unit1 in that
Unit's interface uses clause and call them as Form1.I[x] and
Form1.MyString[x].  Also from Example 2 I would put Unit1 in the other
units uses clause and just call them as I[x] and MyString[x].  But in
example 1 there is no effect on the Data Size for the program whereas in
example 2 the Data Size increases substantially (actually kills the
program, "Data Size to large").  Where are the variables in example 1
stored and which is the better way to go programatically (ignoring the fact
that the second one crashes)??  Are there limits on how much memory is
available for variables declared as per example 1??  Why am I working so
late?

  Any insight into this would be greatly appreciated!!  

--
Rod Geraghty (:>)
GERA-Tech
Ottawa, Canada
gera...@ibm.net

 

Re:What's the difference? Memory Question.


Quote
Rodney E Geraghty wrote:

> Hello all,  figured I would post a question for a change.

> What is the difference between the two following code segments?

> *****Example 1*****

> type
>   TForm1 = class(TForm)
>     Button1: TButton;
>     Button2: TButton;
>     Edit1: TEdit;
>     Edit2: TEdit;
>     procedure Button1Click(Sender: TObject);
>     procedure Button2Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>     I : Array[1..100] of Integer;                    {<---Question}
>     MyString :  Array[1..100] of String;        {<---Question}
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> *****Example 2*****

> type
>   TForm1 = class(TForm)
>     Button1: TButton;
>     Button2: TButton;
>     Edit1: TEdit;
>     Edit2: TEdit;
>     procedure Button1Click(Sender: TObject);
>     procedure Button2Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;
>   I : Array[1..100] of Integer;                    {<---Question}
>   MyString :  Array[1..100] of String;        {<---Question}

> implementation

> {$R *.DFM}

> *******

> I am aware that if I want to use the variables I[x] and MyString[x] in
> another unit from the first example then I would have to put Unit1 in that
> Unit's interface uses clause and call them as Form1.I[x] and
> Form1.MyString[x].  Also from Example 2 I would put Unit1 in the other
> units uses clause and just call them as I[x] and MyString[x].  But in
> example 1 there is no effect on the Data Size for the program whereas in
> example 2 the Data Size increases substantially (actually kills the
> program, "Data Size to large").  Where are the variables in example 1
> stored and which is the better way to go programatically (ignoring the fact
> that the second one crashes)??  Are there limits on how much memory is
> available for variables declared as per example 1??  Why am I working so
> late?

>   Any insight into this would be greatly appreciated!!

The first example will dynamically allocate the needed size, when an
instance of TForm1 is created. It'll also allow you to use multiple
instances of TForm1, each having a separate set of I and MyString.

The second example places the variables in your data segment (Can i call
this static allocation??) These variables are shared among all instances
of TForm1 and all code using the unit. I haven't really looked into D2/3
long strings, but I can imagine they don't like being in the data
segment, as they resizes dynamically.

The first way is normally the best, dynamic allocation makes your
program consume only the memory it need at any given moment. There are
lots of situations in which you must use global variables, but if you
can avoid them, do so. Also, don't store e.g. an array of 100 integers
in a global variable, but rather store a pointer to the array in the
variable, and allocate the needed memory in your units initialization,
using New or GetMem.

Regards,

Erik.
--
Need a custom component? Late on a project? Could use an util?
DOS Device driver? A VxD? NT drivers or services?
Applications of any kind?
Low rates, fast delivery!

When responding to news postings, please CC a copy to my email address.
Thanks.
Erik Sperling Johansen <e...@info-pro.no>

Re:What's the difference? Memory Question.


Hi Rod,
In the first example you have put your arrays inside an object (the
form) which is created dynamically at application startup, and it uses
memory of the heap.
In the second example you are defining static arrays in the
datasegment which will be a part of the compiled code.

Geir

On 14 May 97 04:26:30 GMT, "Rodney E Geraghty" <gera...@ibm.net>
wrote:

Quote
>Hello all,  figured I would post a question for a change.

>What is the difference between the two following code segments?

>*****Example 1*****

>type
>  TForm1 = class(TForm)
>    Button1: TButton;
>    Button2: TButton;
>    Edit1: TEdit;
>    Edit2: TEdit;
>    procedure Button1Click(Sender: TObject);
>    procedure Button2Click(Sender: TObject);
>  private
>    { Private declarations }
>  public
>    { Public declarations }
>    I : Array[1..100] of Integer;                    {<---Question}
>    MyString :  Array[1..100] of String;        {<---Question}
>  end;

>var
>  Form1: TForm1;

>implementation

>{$R *.DFM}

>*****Example 2*****

>type
>  TForm1 = class(TForm)
>    Button1: TButton;
>    Button2: TButton;
>    Edit1: TEdit;
>    Edit2: TEdit;
>    procedure Button1Click(Sender: TObject);
>    procedure Button2Click(Sender: TObject);
>  private
>    { Private declarations }
>  public
>    { Public declarations }
>  end;

>var
>  Form1: TForm1;
>  I : Array[1..100] of Integer;                    {<---Question}
>  MyString :  Array[1..100] of String;        {<---Question}

>implementation

>{$R *.DFM}

>*******

>I am aware that if I want to use the variables I[x] and MyString[x] in
>another unit from the first example then I would have to put Unit1 in that
>Unit's interface uses clause and call them as Form1.I[x] and
>Form1.MyString[x].  Also from Example 2 I would put Unit1 in the other
>units uses clause and just call them as I[x] and MyString[x].  But in
>example 1 there is no effect on the Data Size for the program whereas in
>example 2 the Data Size increases substantially (actually kills the
>program, "Data Size to large").  Where are the variables in example 1
>stored and which is the better way to go programatically (ignoring the fact
>that the second one crashes)??  Are there limits on how much memory is
>available for variables declared as per example 1??  Why am I working so
>late?

>  Any insight into this would be greatly appreciated!!  

>--
>Rod Geraghty (:>)
>GERA-Tech
>Ottawa, Canada
>gera...@ibm.net

Other Threads