When the variable freed in Optimize compile

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

When the variable freed in Optimize compile

zaher dirkey
Hi, I know when compile the project with optimize in Delphi the variable freed/allocated after the last line used.
How can i make same in FPC, O3 not worked for me.
I used FPC 2.4.4

var
  c: Currency;
begin
   c := 10;
   writeln(c);
<<now c freed
   some code here
end;

Thanks in advance.
--
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When the variable freed in Optimize compile

Jonas Maebe-2

On 08 Jun 2011, at 10:57, Zaher Dirkey wrote:

> Hi, I know when compile the project with optimize in Delphi the  
> variable
> freed/allocated after the last line used.
> How can i make same in FPC, O3 not worked for me.

FPC only reuses stack slots allocated for temporary expressions. It  
does not reuse stack slots used by variables.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When the variable freed in Optimize compile

michael.vancanneyt
In reply to this post by zaher dirkey


On Wed, 8 Jun 2011, Zaher Dirkey wrote:

> Hi, I know when compile the project with optimize in Delphi the variable
> freed/allocated after the last line used.
> How can i make same in FPC, O3 not worked for me.
> I used FPC 2.4.4
>
> var
>  c: Currency;
> begin
>   c := 10;
>   writeln(c);
> <<now c freed
>   some code here
> end;

C (as a currency) is never 'freed'. It is declared throughout the whole procedure.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When the variable freed in Optimize compile

zaher dirkey
On Wed, Jun 8, 2011 at 11:22 AM, <[hidden email]> wrote:


On Wed, 8 Jun 2011, Zaher Dirkey wrote:

Hi, I know when compile the project with optimize in Delphi the variable
freed/allocated after the last line used.
How can i make same in FPC, O3 not worked for me.
I used FPC 2.4.4

var
 c: Currency;
begin
 c := 10;
 writeln(c);
<<now c freed
 some code here
end;

C (as a currency) is never 'freed'. It is declared throughout the whole procedure.

What if C is string or Interface of TInterfacedObject?

Thanks

--
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When the variable freed in Optimize compile

Martin Friebe
On 08/06/2011 11:37, Zaher Dirkey wrote:
On Wed, Jun 8, 2011 at 11:22 AM, <[hidden email]> wrote:


On Wed, 8 Jun 2011, Zaher Dirkey wrote:

Hi, I know when compile the project with optimize in Delphi the variable
freed/allocated after the last line used.
How can i make same in FPC, O3 not worked for me.
I used FPC 2.4.4

var
 c: Currency;
begin
 c := 10;
 writeln(c);
<<now c freed
 some code here
end;

C (as a currency) is never 'freed'. It is declared throughout the whole procedure.

What if C is string or Interface of TInterfacedObject?


Read the following threads, it has been lengthly discussed before:

http://lists.freepascal.org/lists/fpc-devel/2010-November/022806.html
http://lists.freepascal.org/lists/fpc-devel/2010-November/022867.html

there may be more threads by the same subject


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When the variable freed in Optimize compile

michael.vancanneyt
In reply to this post by zaher dirkey


On Wed, 8 Jun 2011, Zaher Dirkey wrote:

> On Wed, Jun 8, 2011 at 11:22 AM, <[hidden email]> wrote:
>
>>
>>
>> On Wed, 8 Jun 2011, Zaher Dirkey wrote:
>>
>>  Hi, I know when compile the project with optimize in Delphi the variable
>>> freed/allocated after the last line used.
>>> How can i make same in FPC, O3 not worked for me.
>>> I used FPC 2.4.4
>>>
>>> var
>>>  c: Currency;
>>> begin
>>>  c := 10;
>>>  writeln(c);
>>> <<now c freed
>>>  some code here
>>> end;
>>>
>>
>> C (as a currency) is never 'freed'. It is declared throughout the whole
>> procedure.
>>
>
> What if C is string or Interface of TInterfacedObject?

It is the same logic. It's freed only at the end.

For compiler-generated temporary variables, you should not make any assumptions.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When the variable freed in Optimize compile

etrusco
In reply to this post by Jonas Maebe-2
On Wed, Jun 8, 2011 at 6:17 AM, Jonas Maebe <[hidden email]> wrote:

>
> On 08 Jun 2011, at 10:57, Zaher Dirkey wrote:
>
>> Hi, I know when compile the project with optimize in Delphi the variable
>> freed/allocated after the last line used.
>> How can i make same in FPC, O3 not worked for me.
>
> FPC only reuses stack slots allocated for temporary expressions. It does not
> reuse stack slots used by variables.
>
>
> Jonas
>

While we are at it, is this a limitation or by design?

> >   What if C is string or Interface of TInterfacedObject?
> It is the same logic. It's freed only at the end.

I recently noticed that this is also true for ansistrings. I see this
can avoid lots of errors, but also makes it very ackward to
intentionally avoid copies when modifying strings in-place if they are
obtained from a function...

-Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal