Recent

Author Topic: Translating from C  (Read 3241 times)

geraldholdsworth

  • Jr. Member
  • **
  • Posts: 92
Translating from C
« on: June 07, 2021, 12:03:11 pm »
Hi all,

I'm not very proficient in C but I'm trying to translate a block of code to Pascal. I think it's getting unstuck because the original code is using pointers while I'm using arrays.

The original code is:
Code: C  [Select][+][-]
  1.         unsigned long *prefix;
  2.         unsigned char *suffix;
  3.         unsigned char *stack;
  4.         unsigned char *stackptr;
  5.         prefix = (unsigned long*)malloc(sizeof(unsigned long) * (max + 1));
  6.         suffix = (unsigned char*)malloc(sizeof(unsigned char) * (max + 1));
  7.         stack  = (unsigned char*)malloc(sizeof(unsigned char) * (max + 1));
  8.         stackptr = &stack[0];
  9.  
Then the stack is accessed thus (just a random line I picked out):
Code: C  [Select][+][-]
  1. *stackptr++ = firstchar;
prefix and suffix are accessed, however, via:
Code: Pascal  [Select][+][-]
  1. *stackptr++ = suffix[code];
  2. code = prefix[code];
So, onto Pascal and mine is:
Code: Pascal  [Select][+][-]
  1. stackptr   : Cardinal;
  2. prefix     : array of Cardinal;
  3. suffix,
  4. stack      : array of Byte;
and accessed thus:
Code: Pascal  [Select][+][-]
  1. stack[stackptr]:=firstchar;
  2. inc(stackptr);
Now the code doesn't do what it is supposed to, so I'm assuming that the problem may lie here. It could be else where, but I'm eliminating the possible to narrow it down.
Oh, I haven't really delved into using pointers in Pascal before, so this won't help!! :D

geraldholdsworth

  • Jr. Member
  • **
  • Posts: 92
Re: Translating from C
« Reply #1 on: June 07, 2021, 12:46:46 pm »
OK, found the problem...
In the original code we have:
Code: C  [Select][+][-]
  1. /* Output what has been accumulated on the stack */
  2. while (stackptr > stack) {
  3.         stackptr--;
  4.         if (outptr < olen)
  5.         outbuf[outptr++] = *stackptr;
  6. }
and I had written:
Code: Pascal  [Select][+][-]
  1. //Output what has been accumulated on the stack
  2. while stackptr>Length(stack) do
  3. begin
  4.  dec(stackptr);
  5.  if outptr<Length(Result) then
  6.  begin
  7.   Result[outptr]:=stack[stackptr];
  8.   inc(outptr);
  9.  end;
  10. end;
Where it the condition should have been (and now is):
Code: Pascal  [Select][+][-]
  1. while stackptr>0 do
BTW, if anyone is interested, this is code to decompress an LZW block.

jamie

  • Hero Member
  • *****
  • Posts: 4732
Re: Translating from C
« Reply #2 on: June 07, 2021, 09:25:14 pm »
The ZSTREAM unit won't do it for you?
The only true wisdom is knowing you know nothing

geraldholdsworth

  • Jr. Member
  • **
  • Posts: 92
Re: Translating from C
« Reply #3 on: June 07, 2021, 09:29:32 pm »
Couldn't find any information about whether ZStream would do it. I've got it doing GZip and Zip.

jamie

  • Hero Member
  • *****
  • Posts: 4732
Re: Translating from C
« Reply #4 on: June 07, 2021, 09:54:49 pm »
There is the "Zipper" unit which uses zstream unit which houses TZipper and TUnZipper .
The only true wisdom is knowing you know nothing

geraldholdsworth

  • Jr. Member
  • **
  • Posts: 92
Re: Translating from C
« Reply #5 on: June 07, 2021, 11:21:33 pm »
I actually use both Zipper and ZStream in other parts of the project for compressing and decompressing ZIP data and GZip data, respectively.

For the size of the LZW decompression code, it will hardly make much of a difference.

del

  • Full Member
  • ***
  • Posts: 214
Re: Translating from C
« Reply #6 on: June 08, 2021, 02:06:53 pm »
Good luck with pointers in Pascal. The documentation is pretty silly. The "tutorials" typically have a pointer, and a fixed array, and then they'll have the pointer point to the fixed array.   >:(

And there's a weird wrinkle with Move(srcPtr, dstPtr, numBytes). You have to "deference" the pointers to make this work: Move(srcPtr^, dstPtr^,  numBytes). I do a lot of chained floating point image processing and the available image containers don't support floating point. So I have to roll my own. At any rate, if you're fluent in C you might want to try a pure pointer (no dynamic arrays) solution in Pascal.

440bx

  • Hero Member
  • *****
  • Posts: 2429
Re: Translating from C
« Reply #7 on: June 08, 2021, 02:26:13 pm »
Good luck with pointers in Pascal. The documentation is pretty silly.
As much as I dislike C, I have to concede that pointer handling makes a lot more sense in C than in Pascal, particularly when someone had the "bright idea" of creating untyped variables which is what causes the nonsense of having to dereference a pointer to get the pointer to the memory block.  (whoever thought of that one probably smoked red paint for breakfast.)

All that said, if a programmer is disciplined enough to stay away from those "creative" gimmicks, pointer handling in Pascal is, at least quite close to, as flexible and powerful as it is in C.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

del

  • Full Member
  • ***
  • Posts: 214
Re: Translating from C
« Reply #8 on: June 08, 2021, 03:56:27 pm »
Good luck with pointers in Pascal. The documentation is pretty silly.
As much as I dislike C, I have to concede that pointer handling makes a lot more sense in C than in Pascal, particularly when someone had the "bright idea" of creating untyped variables which is what causes the nonsense of having to dereference a pointer to get the pointer to the memory block.  (whoever thought of that one probably smoked red paint for breakfast.)

All that said, if a programmer is disciplined enough to stay away from those "creative" gimmicks, pointer handling in Pascal is, at least quite close to, as flexible and powerful as it is in C.

I'm loving it so far. Other than that little rant I posted. The cool thing is that Heap Trace still works when you do your own pointer stuff.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9441
  • FPC developer.
Re: Translating from C
« Reply #9 on: June 08, 2021, 04:37:56 pm »
I'm already doing image analysis in Pascal for near on 18 years now, 15 years of that full time.

In the early days I had C dlls, but in the end I converted it all to Delphi/FPC long ago. The speed was not measurably different. Usually memory performance is dominant.

I now have C++ DLLs again, but that is not std C++ code, but a powerful combination of templates and intrinsics, mostly from https://ermig1979.github.io/Simd/

The trick they use there for kernel operations is that they use the template code for the bounderies of a kernel operation (but hardwire some parameters to 0) as for the bulk.

del

  • Full Member
  • ***
  • Posts: 214
Re: Translating from C
« Reply #10 on: June 08, 2021, 06:39:59 pm »
I'm already doing image analysis in Pascal for near on 18 years now, 15 years of that full time.

In the early days I had C dlls, but in the end I converted it all to Delphi/FPC long ago. The speed was not measurably different. Usually memory performance is dominant.

I now have C++ DLLs again, but that is not std C++ code, but a powerful combination of templates and intrinsics, mostly from https://ermig1979.github.io/Simd/

The trick they use there for kernel operations is that they use the template code for the bounderies of a kernel operation (but hardwire some parameters to 0) as for the bulk.
Thanks marcov. I did my thesis in Modula2 with a lot of low-level BIOS calls to the video card. But then I kinda moved into the C / C++ world. It's fun to be doing pointers in FPC, I like being down in the weeds.
« Last Edit: June 08, 2021, 07:35:19 pm by del »

 

TinyPortal © 2005-2018