Sign in Join

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

Mailing lists / fpc-pascal / 2012-01

Access violation that I do not understand while creating a class instance

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

ik
to ik
ik wrote 13 lines
Hello, In the following code ( https://github.com/ik5/redit_client.fpc/blob/master/tests/test_parser.lpr#L55), when I point, I have access violation when trying to create TRedisErrorReturnType.Create It does not happen for TRedisStatusReturnType.Create for example, that have the same parent as TRedisErrorReturnType. What am I missing or doing wrong here ?

Solved it, but I do not understand it.
Why did new made access violation and StrAlloc not ?

Show quoted text 5 lines
Thanks, Ido

Reply by email

Henry Vermaak
to ik
ik wrote 19 lines
On Mon, Jan 23, 2012 at 16:28, ik < > wrote: Hello, In the following code (https://github.com/ik5/redit_client.fpc/blob/master/tests/test_parser.lpr#L55), when I point, I have access violation when trying to create TRedisErrorReturnType.Create It does not happen for TRedisStatusReturnType.Create for example, that have the same parent as TRedisErrorReturnType. What am I missing or doing wrong here ? Solved it, but I do not understand it. Why did new made access violation and StrAlloc not ?

Because new() just allocated one pchar and not a buffer. Why do you think using a pchar like this is faster than using string? Have you benchmarked it?

Henry

Reply by email

ik
to Henry Vermaak
Henry Vermaak wrote 29 lines
On 23/01/12 14:56, ik wrote: > On Mon, Jan 23, 2012 at 16:28, ik < > > wrote: > > Hello, > > In the following code > (https://github.com/ik5/redit_**client.fpc/blob/master/tests/** > test_parser.lpr#L55<https://github.com/ik5/redit_client.fpc/blob/master/tests/test_parser.lpr#L55> > ), > > when I point, I have access violation when trying to create > TRedisErrorReturnType.Create > > It does not happen for TRedisStatusReturnType.Create for example, > that have the same parent as TRedisErrorReturnType. > > What am I missing or doing wrong here ? > > > Solved it, but I do not understand it. > Why did new made access violation and StrAlloc not ? > Because new() just allocated one pchar and not a buffer. Why do you think using a pchar like this is faster than using string? Have you benchmarked it?

I haven't benchmark anything in my life :)
PChar is an array like approach. AnsiString, is a record based pointer. It uses more memory, and provide a bit overhead for the same thing. At least on theory. I would love to learn that it's not the case.

Thanks

Show quoted text 6 lines
Henry ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>

Ido

Reply by email

Henry Vermaak
to ik
ik wrote
PChar is an array like approach. AnsiString, is a record based pointer.

AnsiString also stores the string in an array. It is dynamic, though, so it stores the size and also reference count.

It uses more memory, and provide a bit overhead for the same thing. At least on theory. I would love to learn that it's not the case.

The only thing you save on here is reference counting, which is probably negligible. That's why I asked if you benchmarked it. Especially since you've already created a bug in trying to use pchar.

Henry

Reply by email

Jürgen Hestermann
to ik: I haven't benchmark anything in my life :) PChar is an array like approach. AnsiString,…
PChar is an array like approach. AnsiString, is a record based

pointer. It uses more memory, and provide a bit overhead for the same thing.

At least on theory. I would love to learn that it's not the case.

AnsiString stores and keeps track of the string length and this has many advantages:

*) If you need the length of a Pchar string you always need to count character by character (until #0). This is very slow. Ansistring has it available directly.

*) If you compare two strings for identity you can first compare lengths. If different then strings are different too. Not possible with pchar.

*) You can store #0 in AnsiStrings (not possible with pchar because it indicates the end of the string).

*) You can access the last string character directly by using the length. With pchar you have to iterate on all characters.

Reply by email