Recent

Author Topic: Can this C struct be expressed in FPC Pascal ?  (Read 19265 times)

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #30 on: July 06, 2019, 11:01:17 pm »
That video is absolute garbage..
I like both methods of coding but when it comes to managing multiple items of the same thing you can not
beat OOP.
 
 I've done the way of creating multiple forms without OOP and it gets to be a mess!
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #31 on: July 06, 2019, 11:46:51 pm »
I like both methods of coding but when it comes to managing multiple items of the same thing you can not
beat OOP.
Nope.  I think the only thing that might not beat OOP is honest-to-goodness totally out of control, full blown, spaghetti code. That _might_ be worse.

I've done the way of creating multiple forms without OOP and it gets to be a mess!
I believe you.  That's usually what people who don't know how to do it end up creating. 

That video is absolute garbage..
Your reaction is quite typical unfortunately. He makes quite a few good points.  He also missed quite a few of them but, he is much more often right than wrong.

For the record, I only listened to it, I didn't watch it.  I have no idea if he showed something I would consider wrong.


@SymbolicFrank

This one: https://www.youtube.com/watch?v=QM1iUe6IofM

Thank you.  It's nice to know not everyone has been infected by OOP.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #32 on: July 07, 2019, 12:12:11 am »
I used the dynamic arrays as an example because it has similarities with "array[]".  There is no reason for the size of an array to always be known and even now, in FPC, there are arrays of unknown size, specifically, any typed pointer can be used as an array reference.
There are three reasons on top of my head, why the size of an array always have to be known:
1) length() - has to return something. The function has to run on anything declared as an array and must return something
2) rangechecks - which are applicable to anything declared as an array.
3) passing an array to another routine (as an open array)
So, while there are syntax similarities, the semantic is different.

FPC already sees pointers the same way as C, as the address of an array.
you're right. Both treat pointers identically.
But what you're referring to is pointer math, not arrays.
Arrays in C are pretty much pointers. Arrays in Pascal are arrays (with a range, meaning the size is known).

The Pascal language itself still allows you to get C-like behavior (as shown multiple times on the page 1)
but doesn't do that automatically in any manner (by not providing a similar functionality of an array of unknown size)

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #33 on: July 07, 2019, 12:28:55 am »
Code: Pascal  [Select][+][-]
  1.   mystruct = packed record
  2.     a,b: DWORD;
  3.     function c: PDWORD; inline;
  4.   end;
  5.  
  6. function mystruct.c: PDWORD;
  7. begin
  8.   Result := PDWORD(pbyte(@self.b)+sizeOf(self.b));
  9. end;
A tiny bit faster:
Code: Pascal  [Select][+][-]
  1.   mystruct = packed record
  2.     a,b: DWORD;
  3.     function c: PDWORD; inline;
  4.   strict private
  5.     cStart: record end;
  6.   end;
  7.  
  8. function mystruct.c: PDWORD;
  9. begin
  10.   Result := PDWORD(@cStart);
  11. end;

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #34 on: July 07, 2019, 01:33:00 am »
@skalogryz

There are three reasons on top of my head, why the size of an array always have to be known:
1) length() - has to return something. The function has to run on anything declared as an array and must return something
2) rangechecks - which are applicable to anything declared as an array.
3) passing an array to another routine (as an open array)
So, while there are syntax similarities, the semantic is different.
Here are my thoughts on that:

1) length() would always return zero just as it does for a dynamic array before SetLength() has been called to allocate memory for it.

2) rangechecks on such an array is not applicable since there is no range associated with the array.  IOW, whether rangechecks are enabled or not, the compiler would not enforce them on that kind of array since it does not have the information necessary to do any range checking.  For arrays of that kind, range checking would simply ignore them (as it should since it is the programmer's responsibility to ensure there are elements in the range he/she is accessing.)

3) if such an array were to be passed as an open array, the compiler would pass -1 for the element count (what high(array) currently does for an unallocated dynamic array).  The only difference with a dynamic array is that the count of elements isn't kept in a variable, as far as the compiler is concerned, it is always zero by definition.


FPC already sees pointers the same way as C, as the address of an array.
you're right. Both treat pointers identically.
But what you're referring to is pointer math, not arrays.
Arrays in C are pretty much pointers. Arrays in Pascal are arrays (with a range, meaning the size is known).

The Pascal language itself still allows you to get C-like behavior (as shown multiple times on the page 1)
but doesn't do that automatically in any manner (by not providing a similar functionality of an array of unknown size)
Arrays in both C and FPC are pointers (and any language that supports calculating an address based on an element's size.)  The operations allowed differ based on how those pointers are declared, which can be as a pointer with a range (e.g, array[low..high] of something, commonly thought of as an array) and a pointer without a range (p : pchar; just a pointer.)

The distinction between a pointer and an array is really artificial.


@ASerge,

Code: Pascal  [Select][+][-]
  1.   mystruct = packed record
  2.     a,b: DWORD;
  3.     function c: PDWORD; inline;
  4.   strict private
  5.     cStart: record end;
  6.   end;
  7.  
  8. function mystruct.c: PDWORD;
  9. begin
  10.   Result := PDWORD(@cStart);
  11. end;

Faster or not, that is very clean.  Very nice, thank you!.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #35 on: July 07, 2019, 02:18:58 am »
Here are my thoughts on that:
1) ...
2) ...
3) ...
in that case you don't have an array. Or an array is always of a zero size.
Which makes it pretty useless.

The distinction between a pointer and an array is really artificial.
in your case you should really stick to the plain typed-pointer type, rather than array[] of any sort.

FPC allows you to use pointers in array-like manner.

Code: Pascal  [Select][+][-]
  1. var
  2.   a : PDword;
  3.   b : array [0..2] of DWORD;
  4. begin
  5.   b[0]:=0;
  6.   b[1]:=100;
  7.   b[2]:=200;
  8.  
  9.   a := @b[0];
  10.   writeln(a[1]);
  11.   writeln(a[2]);
  12. end.
  13.  
output:
Code: [Select]
100
200

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #36 on: July 07, 2019, 02:31:07 am »
Here are my thoughts on that:
1) ...
2) ...
3) ...
in that case you don't have an array. Or an array is always of a zero size.
Which makes it pretty useless.
No, it is not useless.  It is quite a simple and useful way of marking the start of an array in a variable size record.  The array can be populated just like any array.  If a programmer tells the compiler
Code: Pascal  [Select][+][-]
  1. a[x] := somevalue;
all the compiler has to do is calculate the offset from "a" and stick "somevalue" in there.  It is no more complicated than that and, it already knows how to do it, all it needs is for the programmer to tell it where (the address of) the array is.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #37 on: July 07, 2019, 02:48:23 am »
but this is what typed pointer is already doing

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #38 on: July 07, 2019, 05:36:06 am »
This one: https://www.youtube.com/watch?v=QM1iUe6IofM

That was 45 minutes of my life I will never get back. Yes, I have many libraries with thousand of functions that operate on simple data types such as arrays or matrixes. The multiplication of matrixes, the solution of linear equations, or finding eigenvalues does not require OOP. But OOP simplifies many problems, if I have 10 plots to draw that share 80% of the codebase, that is a no brainer.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #39 on: July 07, 2019, 06:02:41 am »
That was 45 minutes of my life I will never get back.
That's funny. It doesn't matter how you spend any minutes of your life, you'll never get them back no matter how you spent them.

But OOP simplifies many problems, if I have 10 plots to draw that share 80% of the codebase, that is a no brainer.
There are lots of bad programming practices that simplify things, for instance, global variables but, it doesn't mean they are good.  The thing they all have in common is that the cost of the simplicity they bring is paid in complexity in another area but, if one chooses to ignore their cost then, they are great.

The guy in that video is right but, no one is going to listen. He might as well be talking to a wall.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #40 on: July 07, 2019, 06:34:25 am »
That was 45 minutes of my life I will never get back.
That's funny. It doesn't matter how you spend any minutes of your life, you'll never get them back no matter how you spent them.
Thanks, I'm glad you appreciated my joke.

But OOP simplifies many problems, if I have 10 plots to draw that share 80% of the codebase, that is a no brainer.
There are lots of bad programming practices that simplify things, for instance, global variables but, it doesn't mean they are good.  The thing they all have in common is that the cost of the simplicity they bring is paid in complexity in another area but, if one chooses to ignore their cost then, they are great.

The guy in that video is right but, no one is going to listen. He might as well be talking to a wall.
Everyone is free to avoid OOP and use old school FORTRAN, C, Pascal, etc. I find it useful though.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #41 on: July 07, 2019, 09:10:59 am »
Yes, I also think that OOP is one of the best tools we have. You just have to know how to use it.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #42 on: July 07, 2019, 09:58:25 am »
You just have to know how to use it.
That's what OOP programmers tell themselves to feel good.  Those who realize how bad that thing is just don't know anything, it's so much easier that way.

That guy in the video proves the adage "There's none so deaf as those who will not hear."
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #43 on: July 07, 2019, 10:37:08 am »
That video is absolute garbage..
Your reaction is quite typical unfortunately. He makes quite a few good points.  He also missed quite a few of them but, he is much more often right than wrong.

He has some sidepoints that IMHO should lead him to the conclusion that anything driven to extremes just invites trouble. (like if you should invite an overarching OO structure into your OS)

As a result, he just goes to a different extreme, and falls of the rails.

And yes, the video is crap, excrement etc. At least the first few minutes that I could bear. Too much unrelated cases, too much paranoia, too other unrelated stuff dragged in by the hairs.

If you want to make a point about OOP vs procedural, make a point based on building an application in either. Leave out the bosses, winapi, and US-as-a-nazi states pictures etc. (that was when I stopped watching, but I was already skipping ahead). Simply because it is irrelevant and not the level where most people work (and I doubt he has experience on that level either). Stick to what you (and your viewers) hopefully intimately know.

I also think the state minimization argument is weak. That is not a unique aspect of procedural programming, other than that it nowadays attracts minimalists that bother. (Well at least they upgraded from asm. Took only 40 years)

If tomorrow OOP becomes the minimalists next sweet spot, I'm sure they'll use the same arguments :-)

One of the first shots was the most interesting, but afaik he didn't delve deeper into it. I do often feel that information hiding/encapsulation (and then specially investing heavily in _language level_ encapsulation by default) is the least necessary part of OOP practices.

Specially in one (major) programmer projects, people always drone on on how in the future multiple other people might use the codebase and benefit from it, but my experience is that an aspect of the codebase is not stressed on a daily basis, the quality of it (iow classifying encapsulation visibility of fields/methods) will be very low.

OO should be about modelling and design, the language features are only helpers. People tend to overfocus on them.
« Last Edit: July 07, 2019, 10:40:48 am by marcov »

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #44 on: July 07, 2019, 10:54:12 am »
Yes, making a property out of everything isn't mandatory. A public field will do in many cases. Just make it a property if you need to do some bookkeeping when accessing it. And a setter with just reading the field is often a good compromise.

 

TinyPortal © 2005-2018