Am curious to know what the difference is with string allocation in c++ compared to pascal.
How do the strings get allocated?
C++ also has char arrays/char*/const char*, how do these all differ in their allocation and use?
Am curious to know what the difference is with string allocation in c++ compared to pascal.
How do the strings get allocated?
C++ also has char arrays/char*/const char*, how do these all differ in their allocation and use?
A string that consists of a length followed by a sequence of character codes is called a Pascal string. It's more descriptively called a length-prefixed string. For example, a string created with the Windows API's SysAllocString
function, is a length-prefixed string, a.k.a. Pascal string.
A C++ raw string literal instead consists of character codes followed by a nullvalue, a zero terminated string.
As of C++11 and later C++ std::string
has a buffer that can be viewed as a zero-terminated string, but it also has a separate explicit length. It's not specified where either the length or the buffer is stored. This varies between implementations.
Storage for a zero-terminated string or Pascal string can be allocated in any way you wish, dynamically or as a local variable.
With a C++ std::string
the buffer must in general be dynamically allocated, via the the standard allocator that std::string
is equipped with, because the string can be abritrarily large, and because there is no way for client code to supply a buffer.
However, unlike a std::vector
there are no requirements on std::string
that prohibit a fixed size buffer for small enough strings, and so many (most?) implementations now provide the short string optimization. For a short enough string value everything can then be fit directly within the std::string
object. E.g. as a local variable.
There is a C++11 and later constant time requirement on operator[]
for std::string
, which effectively prevents the COW (Copy On Write) shared ownership strategy used by some C++03 implementations.
There are multiple implementations of Pascal strings. The Turbo Pascal string is mostly statically allocated, and the string types that are new in Delphi are dynamic. Delphi strings have a null at the end (but are not null-terminated, the strings can contain null characters), and Turbo Pascal are not. Delphi has 4 or 5 such types, including the Turbo Pascal one.
However both adhere to the same rough template that UCSD Pascal (of bytecode interpreter fame) coined.
In a lot of C-centric literature "Pascal Strings" is usually about one of the key characteristics, storing the length of a string so that retrieving the length or a pointer to the last character is an O(1) operation.
In addition, Delphi/Free Pascal also can fully emulate manual C strings, since that is basically a library construct apart from literal assignment.