News for hackers. Stuff that matters.
Re:Free Pascal by mikewarot on Sunday December 13, 2020 @11:07AM (#25407500)
I've been working through the Advent of Code, doing it all in Lazarus/Free Pascal... the main limitation I've hit so far is in working out the logic, some of the puzzles are tricky. https://github.com/mikewarot/Advent_of_Code_in_Pascal

[Edit] One of the nice things about pascal strings, and variable parameters, is that you can write functions like

getinteger(var s : string):integer;

Which literally pulls a sign plus digits from s, skipping spaces, until it hits a space, comma, etc... s gets shorter, you get the integer, and it just works.

I don't think that type of thing is even possible in C because of null terminated unmanaged strings.

Re:Free Pascal by benibela on Sunday December 13, 2020 @01:13PM (#25408649)
However, you should not actually do that, because changing s reallocates it, so you get O(n^2) runtime

Should be like, getinteger(const s : string; var startPos: sizeint):integer;

Pascal always has the best possible ideas, and then fucks up the implementation.

Re:Free Pascal by mikewarot on Sunday December 13, 2020 @04:24PM (#25410426)
> However, you should not actually do that, because changing s reallocates it, so you get O(n^2) runtime

Confirmed by experiment.

Parsing 1,2,...1000 string 1000 times 0.355 seconds

Parsing 1,2,....10000 string 1000 times 42.835 seconds

A given string might have 1-10 parameters in it, so the actual execution time isn't going to be that bad. I always think in terms of those 10 Mhz 286s, and how much faster it is these days. ;-)

Obviously, if I were reading Gigabytes of text, I might optimize a bit.

Re:Free Pascal by hiker on Sunday December 13, 2020 @12:53PM (#25408446)
Maybe

  int getint(const char **s) {
    int res = 0;
    for (; **s && isspace(**s); (*s)++);
    for (; **s && isdigit(**s); (*s)++)
      res = 10 * res + **s - '0';
    return res;
  }
Re:Free Pascal by mikewarot on Sunday December 13, 2020 @01:20PM (#25408714)
Here's the same code in pascal.

    function getnum(var s : string):integer;
    var
      c : char;
      value : integer;
    begin
      value := 0;
      while (length(s) <> 0) and NOT(s[1] in ['0'..'9']) do
        delete(s,1,1);
      while (length(s) <> 0) and (s[1] in ['0'..'9']) do
      begin
        value := value * 10 + (ord(s[1])-ord('0'));
        delete(s,1,1);
      end;
      getnum := value;
    end;
Re:Free Pascal by benibela on Sunday December 13, 2020 @01:26PM (#25408781)
And that would be faster than the Pascal version

But it is unsafe. Someone might mistype * * s as *s and then bad things happen.

Pascal strings are basically memory safe. With range checking enabled, you can write whatever and it can only access characters in the string.

Ofc, then people disable the range checking in release builds to make it faster

Re:Free Pascal by rzzzt on Sunday December 13, 2020 @12:30PM (#25408239)
For "out" variables, you can increase the indirection and pass a char** (pointer to a pointer) to the method as the argument.

Then the pointed-at pointer can be manipulated within the method body and changes will be visible on the outside as well, giving you both the numeric return value as well as the shortened string as the output. This is what the second argument of the strto{d,f,ld} function does: https://devdocs.io/c/string/byte/strtof

Re:Free Pascal by benibela on Sunday December 13, 2020 @04:13PM (#25410335)
However, such pointers are unsafe, since someone could pass an invalid or null pointer

var/out variables are much safer, since the variable exists at the calling site , is valid and not null.

It is also much safer than C++'s & references. It is very subtle, but rather amazing if you think about it. Because C++ can return a & reference, it can return a reference to a value on the stack frame of the callee, which then becomes invalid. But the var/out variable always exists on caller's site, so it is always safe.

(at least if it a reference to the stack. a reference to the heap could be freed indirectly through another reference. )

Re:Free Pascal by rzzzt on Sunday December 13, 2020 @05:06PM (#25410833)
I am not a heavyweight C++ user (also a very lightweight C and a, uhm... featherweight Pascal user), but I'd think using a C++ reference as a method argument should be as safe as Pascal's var parameters.

Is returning references frequently done in C++? I have only seen it in ostream's << operator-like constructs where you want to chain multiple calls to the same object.

Re:Free Pascal by pjmlp on Sunday December 13, 2020 @07:02PM (#25411805)
Not necessarily, although it is seldom seen I the wild, you can cast a null pointer into a reference.

Another source of issues might be the creation of temporaries.

Yes, in index operator as well.

Re:Free Pascal by benibela on Monday December 14, 2020 @06:28PM (#25424829)
>Is returning references frequently done in C++?

It is rather common to avoid copying returned objects. (perhaps it becomes less common with std::move ?)

For example, the [] operator on the standard collections returns a reference, so the item can be modified

There Pascal has another clever idea. You can overload [] for reading and writing separately. Then x[k] := v becomes x.setItem(k, v)

Re:Free Pascal by mikewarot on Sunday December 13, 2020 @12:51PM (#25408430)
I appreciate that in C pointers are the coin of the realm, I really do appreciate not having to deal all the time, stuff just works.

[Edit] For example, you can also insert into a string, making it longer, without having to worry about it.