C Strings Vs Pascal Strings

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C Strings Vs Pascal Strings

Post 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)
Every good solution is obvious once you've found it.
Schol-R-LEA

Re:C Strings Vs Pascal Strings

Post 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.
Post Reply