Sign in Join

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

Mailing lists / fpc-pascal / 2012-12

length for long ansistring = segment fault

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

ik

Hello,

When I try to get the length of AnsiString, it returns me segment fault. I'm not sure if I'm doing something wrong:

var
  s : AnsiString;
  l : word;
begin
  SetLength(s, 1024);
  FillChar(s, 1024, '*');
  l := Length(s); // seg fault here
  writeln(l);
end.

Am I doing here something wrong ?

fpc 2.6.0 x86_64 linux

Thanks,
Ido

Michael Van Canneyt
to ik
ik wrote 12 lines
Hello, When I try to get the length of AnsiString, it returns me segment fault. I'm not sure if I'm doing something wrong: var s : AnsiString; l : word; begin SetLength(s, 1024); FillChar(s, 1024, '*');
You are overwriting the pointer. S is (behind the scenes) a pointer to a memory area.
You should do FillChar(s[1], 1024, '*');

Michael.

ik
to Michael Van Canneyt
Michael Van Canneyt wrote 20 lines
On Mon, 3 Dec 2012, ik wrote: > Hello, > > When I try to get the length of AnsiString, it returns me segment fault. > I'm not sure if I'm doing something wrong: > > var > s : AnsiString; > l : word; > > begin > SetLength(s, 1024); > FillChar(s, 1024, '*'); You are overwriting the pointer. S is (behind the scenes) a pointer to a memory area. You should do FillChar(s[1], 1024, '*');

Right :( Thanks

Show quoted text 5 lines
Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to Michael Van Canneyt: You are overwriting the pointer. S is (behind the scenes) a pointer to a memory area. You…
Michael Van Canneyt wrote 12 lines
On Mon, 3 Dec 2012, ik wrote: > var > s : AnsiString; > l : word; > > begin > SetLength(s, 1024); > FillChar(s, 1024, '*'); You are overwriting the pointer. S is (behind the scenes) a pointer to a memory area. You should do FillChar(s[1], 1024, '*');

Or use http://www.freepascal.org/docs-html/rtl/system/stringofchar.html

Jonas

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 17 lines
On 03 Dec 2012, at 11:13, Michael Van Canneyt wrote: > On Mon, 3 Dec 2012, ik wrote: > >> var >> s : AnsiString; >> l : word; >> >> begin >> SetLength(s, 1024); >> FillChar(s, 1024, '*'); > > You are overwriting the pointer. S is (behind the scenes) a pointer to a > memory area. You should do FillChar(s[1], 1024, '*'); Or use http://www.freepascal.org/docs-html/rtl/system/stringofchar.html

I was considering answering with that too, but I didn't feel like checking how it is implemented, to judge the speed penalty :)

Michael.

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 11 lines
On Mon, 3 Dec 2012, Jonas Maebe wrote: > On 03 Dec 2012, at 11:13, Michael Van Canneyt wrote: > >> You are overwriting the pointer. S is (behind the scenes) a >> pointer to a memory area. You should do FillChar(s[1], 1024, '*'); > > Or use http://www.freepascal.org/docs-html/rtl/system/stringofchar.html I was considering answering with that too, but I didn't feel like checking how it is implemented, to judge the speed penalty :)

It definitely doesn't have the "ask the mailing list because I made a mistake"-penalty :)

Jonas

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 17 lines
On 03 Dec 2012, at 11:52, Michael Van Canneyt wrote: > On Mon, 3 Dec 2012, Jonas Maebe wrote: > >> On 03 Dec 2012, at 11:13, Michael Van Canneyt wrote: >> >>> You are overwriting the pointer. S is (behind the scenes) a pointer to a >>> memory area. You should do FillChar(s[1], 1024, '*'); >> >> Or use http://www.freepascal.org/docs-html/rtl/system/stringofchar.html > > I was considering answering with that too, but I didn't feel like checking > how it is implemented, to judge the speed penalty :) It definitely doesn't have the "ask the mailing list because I made a mistake"-penalty :)

Checked anyway now.
Stringofchar uses fillchar internally, so it's better to use that.

Michael.

ik
to Michael Van Canneyt
Michael Van Canneyt wrote 28 lines
On Mon, 3 Dec 2012, Jonas Maebe wrote: > > On 03 Dec 2012, at 11:52, Michael Van Canneyt wrote: > >> On Mon, 3 Dec 2012, Jonas Maebe wrote: >> >>> On 03 Dec 2012, at 11:13, Michael Van Canneyt wrote: >>> >>>> You are overwriting the pointer. S is (behind the scenes) a pointer to >>>> a memory area. You should do FillChar(s[1], 1024, '*'); >>> >>> >>> Or use http://www.freepascal.org/docs-html/rtl/system/stringofchar.html >> >> >> I was considering answering with that too, but I didn't feel like >> checking how it is implemented, to judge the speed penalty :) > > > It definitely doesn't have the "ask the mailing list because I made a > mistake"-penalty :) Checked anyway now. Stringofchar uses fillchar internally, so it's better to use that.

I completely forgot about it, if I would remember it, I would have used it in the first place

Show quoted text 6 lines
Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal