Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-01

Passing an array to a function

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

I have a function that I'm trying to pass an array to.

function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize:
integer): string;

Calling the function:

setlength(fsbuffer,recordsize);
FS:=TFileStream.Create(fname, fmshareDenyWrite);
FS.Seek(seekpos,soFromBeginning);
FS.ReadBuffer(fsbuffer,recordsize);
dfstring:= FieldValueAsString(fsbuffer,fitype,fisize);

Generates the error "Project raised exception class SIGSEGV"

Reply by email

Andrew Brunner
to Dave Coventry

Hi Dave,

My first comment would be you must declare the input as a variable. Next, I probably would type the Array out as well before using it.

Type TByteBuffer=Array of Byte;
function FieldValueAsString(var fsbuffer:TByteBuffer; fitype,fisize:
integer): string;

Byte arrays should not generate an error. There may be a memory leak for instances where you pass them in as constant values. Because the Memory manager would need to free that memory as it is a dynamic array.

-Andy

Dave Coventry wrote 19 lines
I have a function that I'm trying to pass an array to. function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize: integer): string; Calling the function: setlength(fsbuffer,recordsize); FS:=TFileStream.Create(fname, fmshareDenyWrite); FS.Seek(seekpos,soFromBeginning); FS.ReadBuffer(fsbuffer,recordsize); dfstring:= FieldValueAsString(fsbuffer,fitype,fisize); Generates the error "Project raised exception class SIGSEGV" _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Martin Friebe
to Dave Coventry: I have a function that I'm trying to pass an array to. function…

Not sure, but I would try something like

FS.ReadBuffer(fsbuffer[0],recordsize);

The function is declared as
ReadBuffer(var Buffer; ....

So in reality a pointer to the variable is passed to the function. Your dyn-array is already a pointer. You are passing in a pointer to the internal pointer, and not to the bytes you allocated.

Martin

Dave Coventry wrote 19 lines
I have a function that I'm trying to pass an array to. function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize: integer): string; Calling the function: setlength(fsbuffer,recordsize); FS:=TFileStream.Create(fname, fmshareDenyWrite); FS.Seek(seekpos,soFromBeginning); FS.ReadBuffer(fsbuffer,recordsize); dfstring:= FieldValueAsString(fsbuffer,fitype,fisize); Generates the error "Project raised exception class SIGSEGV" _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Dave Coventry
to Andrew Brunner: My first comment would be you must declare the input as a variable. Next, I probably…

Hi Andy, thanks for the response.

The array was declared as
var fsbuffer: array of Byte;
I tried your suggestion and typed the array as
Type TByteBuffer=Array of Byte;
then declared the buffer
var fsbuffer: TByteBuffer;

I also declared the function using 'var' as you suggested

function FieldValueAsString(var fsbuffer: TByteBuffer; fitype,fisize:
integer): string;

But my app is still falling over at the same point.

2009/1/19 Andrew Brunner :

Show quoted text 44 lines
Hi Dave, My first comment would be you must declare the input as a variable. Next, I probably would type the Array out as well before using it. Type TByteBuffer=Array of Byte; function FieldValueAsString(var fsbuffer:TByteBuffer; fitype,fisize: integer): string; Byte arrays should not generate an error. There may be a memory leak for instances where you pass them in as constant values. Because the Memory manager would need to free that memory as it is a dynamic array. -Andy On Mon, Jan 19, 2009 at 8:20 AM, Dave Coventry wrote: > I have a function that I'm trying to pass an array to. > > function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize: > integer): string; > > Calling the function: > > setlength(fsbuffer,recordsize); > FS:=TFileStream.Create(fname, fmshareDenyWrite); > FS.Seek(seekpos,soFromBeginning); > FS.ReadBuffer(fsbuffer,recordsize); > dfstring:= FieldValueAsString(fsbuffer,fitype,fisize); > > Generates the error "Project raised exception class SIGSEGV" > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus > _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Dave Coventry
to Dave Coventry

Thanks Martin, that works.

2009/1/19 Dave Coventry :

Show quoted text 64 lines
Hi Andy, thanks for the response. The array was declared as var fsbuffer: array of Byte; I tried your suggestion and typed the array as Type TByteBuffer=Array of Byte; then declared the buffer var fsbuffer: TByteBuffer; I also declared the function using 'var' as you suggested function FieldValueAsString(var fsbuffer: TByteBuffer; fitype,fisize: integer): string; But my app is still falling over at the same point. 2009/1/19 Andrew Brunner : > Hi Dave, > > > My first comment would be you must declare the input as a variable. > Next, I probably would type the Array out as well before using it. > > Type TByteBuffer=Array of Byte; > > function FieldValueAsString(var fsbuffer:TByteBuffer; fitype,fisize: > integer): string; > > Byte arrays should not generate an error. There may be a memory leak > for instances where you pass them in as constant values. Because the > Memory manager would need to free that memory as it is a dynamic > array. > > -Andy > > > On Mon, Jan 19, 2009 at 8:20 AM, Dave Coventry wrote: >> I have a function that I'm trying to pass an array to. >> >> function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize: >> integer): string; >> >> Calling the function: >> >> setlength(fsbuffer,recordsize); >> FS:=TFileStream.Create(fname, fmshareDenyWrite); >> FS.Seek(seekpos,soFromBeginning); >> FS.ReadBuffer(fsbuffer,recordsize); >> dfstring:= FieldValueAsString(fsbuffer,fitype,fisize); >> >> Generates the error "Project raised exception class SIGSEGV" >> _______________________________________________ >> Lazarus mailing list >> >> http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >> > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >

Reply by email

Andrew Brunner
to Dave Coventry: Hi Andy, thanks for the response. The array was declared as var fsbuffer: array of Byte;…

Ok,

But when reading or writing to the buffer you do need to reference the element in which you want to start writing/reading... In the case that entire buffer is to be written use [0].

You can't write the buffer itself as it's just a pointer...

Dave Coventry wrote 68 lines
Hi Andy, thanks for the response. The array was declared as var fsbuffer: array of Byte; I tried your suggestion and typed the array as Type TByteBuffer=Array of Byte; then declared the buffer var fsbuffer: TByteBuffer; I also declared the function using 'var' as you suggested function FieldValueAsString(var fsbuffer: TByteBuffer; fitype,fisize: integer): string; But my app is still falling over at the same point. 2009/1/19 Andrew Brunner : > Hi Dave, > > > My first comment would be you must declare the input as a variable. > Next, I probably would type the Array out as well before using it. > > Type TByteBuffer=Array of Byte; > > function FieldValueAsString(var fsbuffer:TByteBuffer; fitype,fisize: > integer): string; > > Byte arrays should not generate an error. There may be a memory leak > for instances where you pass them in as constant values. Because the > Memory manager would need to free that memory as it is a dynamic > array. > > -Andy > > > On Mon, Jan 19, 2009 at 8:20 AM, Dave Coventry wrote: >> I have a function that I'm trying to pass an array to. >> >> function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize: >> integer): string; >> >> Calling the function: >> >> setlength(fsbuffer,recordsize); >> FS:=TFileStream.Create(fname, fmshareDenyWrite); >> FS.Seek(seekpos,soFromBeginning); >> FS.ReadBuffer(fsbuffer,recordsize); >> dfstring:= FieldValueAsString(fsbuffer,fitype,fisize); >> >> Generates the error "Project raised exception class SIGSEGV" >> _______________________________________________ >> Lazarus mailing list >> >> http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >> > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus > _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Darmawan Sugiarto

Can you give more information about your function.

=============INTOSOFT============

ALEXIO CHAOS

Yahoo Messager

=================================

--- On Mon, 1/19/09, Dave Coventry wrote:

Earlier message 19 lines
From: Dave Coventry Subject: [Lazarus] Passing an array to a function To: Date: Monday, January 19, 2009, 6:20 AM I have a function that I'm trying to pass an array to. function FieldValueAsString(fsbuffer: array of Byte; fitype,fisize: integer): string; Calling the function: setlength(fsbuffer,recordsize); FS:=TFileStream.Create(fname, fmshareDenyWrite); FS.Seek(seekpos,soFromBeginning); FS.ReadBuffer(fsbuffer,recordsize); dfstring:= FieldValueAsString(fsbuffer,fitype,fisize); Generates the error "Project raised exception class SIGSEGV"

Reply by email

Dave Coventry
to Darmawan Sugiarto

2009/1/20 Darmawan Sugiarto :

Can you give more information about your function.

Sure:

function FieldValueAsString(var fsbuffer: TByteBuffer;
fitype,marker,fisize: integer): string;
begin
  case fitype of
43,52,108:Result:=inttostr(LEtoN(PLongint(@fsbuffer[marker])^));
 50:Result:=inttostr(LEtoN(PSmallInt(@fsbuffer[marker])^));
end;
end;

It was failing before it got there, so I don't think the problem was with the function.

In any case, Martin's suggestion regarding calling the ReadBuffer function with the first element of the array rather than the pointer has worked...

Reply by email

Darmawan Sugiarto

Congratulation, so this case was closed

=============INTOSOFT============

ALEXIO CHAOS

Yahoo Messager

=================================

--- On Mon, 1/19/09, Dave Coventry wrote:

Earlier message 26 lines
From: Dave Coventry Subject: Re: [Lazarus] Passing an array to a function To: "General mailing list" Date: Monday, January 19, 2009, 9:34 PM 2009/1/20 Darmawan Sugiarto : > Can you give more information about your function. Sure: function FieldValueAsString(var fsbuffer: TByteBuffer; fitype,marker,fisize: integer): string; begin ? case fitype of 43,52,108:Result:=inttostr(LEtoN(PLongint(@fsbuffer[marker])^)); 50:Result:=inttostr(LEtoN(PSmallInt(@fsbuffer[marker])^)); end; end; It was failing before it got there, so I don't think the problem was with the function. In any case, Martin's suggestion regarding calling the ReadBuffer function with the first element of the array rather than the pointer has worked...

Reply by email