Einzelnen Beitrag anzeigen

Benutzerbild von d3g
d3g

Registriert seit: 21. Jun 2002
602 Beiträge
 
#1

Warum ist PChar ^= @String[1]?

  Alt 4. Jan 2003, 21:08
Ich bin gerade dabei meine C-Kenntnisse zu vertiefen und dabei ist mir etwas aufgefallen. Ein PChar entspricht einem char* in C(++), sein Ende wird durch #0 angezeigt. Ein String ist ebenfalls ein Pointer auf ein Char, jedoch sind 8 Byte Daten, unter anderem die Länge des Strings:
Zitat von Kylix 3 Hilfe zu 'Long Strings':
AnsiString, also called a long string, represents a dynamically allocated string whose maximum length is limited only by available memory.
A long-string variable is a pointer occupying four bytes of memory. When the variable is empty--that is, when it contains a zero-length string--the pointer is nil and the string uses no additional storage. When the variable is nonempty, it points a dynamically allocated block of memory that contains the string value. The eight bytes before the location contain a 32-bit length indicator and a 32-bit reference count. This memory is allocated on the heap, but its management is entirely automatic and requires no user code.
Der Pascalstring ist nicht nullterminiert (sonst könnte man ja Pascalstrings auch einfach API-Funktionen übergeben).

Daraus ergibt sich mein Problem @String[1] ist ein char* aber woher soll eine API-Funktion wissen, wie lang der String ist, wenn nicht zufällig im Speicherbereich danach binäre Nullen stehen?

Danke im Voraus,
d3g
-- Crucifixion?
-- Yes.
-- Good. Out of the door, line on the left, one cross each.
  Mit Zitat antworten Zitat