Page 3 of 3

Re:C Strings Vs Pascal Strings

Posted: Fri Aug 20, 2004 1:16 pm
by Solar
Curufir wrote: Because I can leverage the fact that I know the lengths to perform operations on dwords not bytes more efficiently.
Sorry but your pseudo-code stands an excellent chance for being of abysmal performance. How can you be sure str2 is dword-aligned in the first place? You know how IA32 memory access degrades on non-aligned values...
Since there's variable character width allowed strlen is meaningless in terms of memory allocation.
I was referring to strlen() for simplicity. Sure, if you work with wide characters, you'd use wcslen(), but that's beside the point, isn't it?
It'd make far more sense to tweak sizeof to return the true size (In bytes) for a string and use that for malloc.

Ok, so it breaks the standard, but only a little ;D.
No, it would heavily break the language. The size of a char* is very much unlike the size of a char[], and how do you intend to avoid general havoc as soon as the user works with typedefs and is blissfully unaware that the uint8_t* suddenly has a sizeof() in the hundreds?

Don't touch the language. Expand it by custom headers and functions, but don't touch the language people came to expect. IMHO, of course.
(Ever seen your compiler use SSE for strings?).
I'd rather not, since that would require my dispatcher to save the SSE registers even for the most trivial of processes, instead of leaving them alone except for those processes that actually use them... (context switch times)

Re:C Strings Vs Pascal Strings

Posted: Fri Aug 20, 2004 2:38 pm
by Schol-R-LEA
Keep in mind that while virtually every Pascal implementation (and Wirth's later languages) provide dynamically resizable arrays and/or a string type, the Pascal array system was not designed with this in mind. In the original Standard Pascal, array size was a part of the array's type. There was nothing to maintain; the array or string size was a constant, and functions pretty much had to be specific to arrays of a given size.

IIRC, even if you used dynamically allocated arrays, the array's size had to match that which the pointer accessing it was declared for:

Code: Select all

VAR
    myString: ^array of char[80];

BEGIN
    new(myString);
   { do something ... }
   dispose(myString)
END.
the new() operator always allocates one variable of the size the pointer is declared for, period. Newer Pascal implementations may alter this, but the language in it's original form was very specifically limited in this manner.

C&CW, especially since it's been some time since I coded in Pascal regularly.