Michael Müller | 1 May 2006 08:11
Picon

Re: const parameter writeable

On Sat, Apr 29, 2006 at 11:20:04PM +0200, Peter Vreman
<peter@...> wrote:
> >
> > Are const parameters supposed to ensure read only access?
> > If so how come one can write to a typed pointer?
> >
> > program project1;
> >
> > {$mode objfpc}{$H+}
> >
> >   type
> >     PSomeRec = ^TSomeRec;
> >     TSomeRec = record
> >       a: string;
> >       b: string;
> >     end;
> >
> >   procedure ChangeRec1(const Rec: PSomeRec);
> >   begin
> >     Rec^.a:= 'string A';
> >   end;
> >
> > var
> >   RecPtr: PSomeRec;
> > begin
> >   new(RecPtr);
> >   ChangeRec1(RecPtr);
> >   writeln(RecPtr^.a);
> >   dispose(RecPtr);
> >   readln;
(Continue reading)

list | 1 May 2006 08:22
Picon
Favicon

Re: const parameter writeable

Michael says:
> - 'const': gives pointer, content read-only

In practice the compiler may[1] check for access to the value at compile
time and just give you a pointer to the real thing (since it knows you
won't change it). With a non-specified parameter it has to make a copy and
pass that, which slows everything down. Especially for big parameters, and
you shouldn't pass big structs around for that reason.

Moz
[1] I assume FPC does this, but don't know for sure.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Jonas Maebe | 1 May 2006 09:58
Picon
Favicon

Re: const parameter writeable


On 01 May 2006, at 08:11, Michael Müller wrote:

> - 'const': gives pointer, content read-only

The compiler can handle const parameters in any way it wants, and it  
may even differ among different cpu's and calling conventions.  
Sometimes they are passed by value, sometimes by reference. It  
depends on what the compiler thinks will be most efficient, or on  
what a calling convention requires.

Since the content is not writable (except by using ugly tricks, in  
which case the programmer himself is responsible for messing up  
things), that does not matter.

> - 'out': I'm not sure if it's more similar to 'var' or ''.

It's like var, except that in some cases the contents of the "out"  
parameter are erased before the function call, and that you get  
different warnings regarding the initialised status of an out  
parameter and the values that you pass to them.

Jonas_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

smartkenny | 1 May 2006 03:11
Picon

Problem with compile Gtk2 for version 2.6

Hello, all

I tried to compile gtk2 for arm linux.
I download the package/extra/gtk2 source from svn server and I use the 
following command to compile it:

make OS_TARGET="linux" CPU_TARGET="arm" FPCCPUOPT="O2" PP="ppcarm" 
COMPILER_OPTIONS="-CX -XX -Xs"

With the original source (GTK2_2), it compiles successfully.

Then I modifed gtk2/gtk+/gtk/gtk2.pas, and changed the following line 
{$define GTK2_2} to 2_4 or 2_6.
The compilation errors are:

Assembling gtk2
Assembling with smartlinking gtk2
/bin/sh: /usr/bin/ar: Argument list too long
gtkdebug.inc(19,6) Error: Error while linking
gtkdebug.inc(19,6) Fatal: There were 1 errors compiling module, stopping
gtkdebug.inc(28,36) Error: Compilation aborted
make: *** [buildgtk2.ppu] Error 1

Can anybody help me to solve it?
Thank you so much.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

(Continue reading)

Marco van de Voort | 1 May 2006 12:25
Picon
Favicon

Re: Problem with compile Gtk2 for version 2.6

> I tried to compile gtk2 for arm linux.
> I download the package/extra/gtk2 source from svn server and I use the 
> following command to compile it:
> 
> make OS_TARGET="linux" CPU_TARGET="arm" FPCCPUOPT="O2" PP="ppcarm" 
> COMPILER_OPTIONS="-CX -XX -Xs"
> 
> With the original source (GTK2_2), it compiles successfully.
> 
> Then I modifed gtk2/gtk+/gtk/gtk2.pas, and changed the following line 
> {$define GTK2_2} to 2_4 or 2_6.
> The compilation errors are:
> 
> Assembling gtk2
> Assembling with smartlinking gtk2
> /bin/sh: /usr/bin/ar: Argument list too long
> gtkdebug.inc(19,6) Error: Error while linking
> gtkdebug.inc(19,6) Fatal: There were 1 errors compiling module, stopping
> gtkdebug.inc(28,36) Error: Compilation aborted
> make: *** [buildgtk2.ppu] Error 1
> 
> Can anybody help me to solve it?

Do not smartlink that unit, iow remove the -CX 
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

L505 | 1 May 2006 20:07
Favicon

Re: const parameter writeable


> > Only the pointer itself is the parameter and read-only. Where the pointer
> > points to is irrelevant.

I remember this from using classes where you can still access the class properties -
should have thought about that before posing the question :).

>
> But in this case I'm wondering why you want to give a pointer instead
> of the real type?

did you mean this ? :

   procedure ChangeRec1(const Rec: TSomeRec);
   begin
     Rec.a:= 'string A';
   end;

Because I can't pass a PSomeRec to that function, only a TSomeRec

Otherwise, I don't know what you mean..

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Jonas Maebe | 1 May 2006 21:56
Picon
Favicon

Re: const parameter writeable


On 01 May 2006, at 20:07, L505 wrote:

> did you mean this ? :
>
>    procedure ChangeRec1(const Rec: TSomeRec);
>    begin
>      Rec.a:= 'string A';
>    end;
>
> Because I can't pass a PSomeRec to that function, only a TSomeRec

Then pass a PSomeRec^ to it if you don't intend to change the pointer  
anyway.

Jonas
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

L505 | 2 May 2006 05:06
Favicon

Re: const parameter writeable


> 
> > did you mean this ? :
> >
> >    procedure ChangeRec1(const Rec: TSomeRec);
> >    begin
> >      Rec.a:= 'string A';
> >    end;
> >
> > Because I can't pass a PSomeRec to that function, only a TSomeRec
> 
> Then pass a PSomeRec^ to it if you don't intend to change the pointer  
> anyway.
> 

That works, I was getting sloppy with Delphi style code who doesn't always require the ^
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Michael Müller | 2 May 2006 08:00
Picon

Re: const parameter writeable

On Mon, May 01, 2006 at 12:07:55PM -0600, L505 <fpc505@...> wrote:
> 
> > > Only the pointer itself is the parameter and read-only. Where the pointer
> > > points to is irrelevant.
> 
> I remember this from using classes where you can still access the class properties -
> should have thought about that before posing the question :).
> 
> >
> > But in this case I'm wondering why you want to give a pointer instead
> > of the real type?
> 
> 
> did you mean this ? :
> 
>    procedure ChangeRec1(const Rec: TSomeRec);
>    begin
>      Rec.a:= 'string A';
>    end;

Yes.

> Because I can't pass a PSomeRec to that function, only a TSomeRec

Where is the problem? If you allocated the memory by new() as in your
example you could call
ChangeRec1(RecPtr^);
and the compile (hopefully) uses the pointer.

But in this case you will get an compiler error because you can't
(Continue reading)

Michael Müller | 2 May 2006 08:07
Picon

Constant object

How can I declare a constant object?

var
  MyTest: TObject;
begin
  MyTest := TObject.Create;
end.

works (for sure).

How can I declare 'const'?

Thanks

Michael
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Gmane