Recent

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

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #15 on: July 06, 2019, 05:38:13 pm »

Posted by: SymbolicFrank
« on: Today at 05:30:46 pm »
Insert Quote

If you really want to program in Pascal like in C, use pointers. Don't rape the language.

You may think that, but C actually does have some value that should be supported In pascal, no matter
how you want to look at it.

 They don't need to use the same syntax but at least produce the same results.
The only true wisdom is knowing you know nothing

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #16 on: July 06, 2019, 06:08:58 pm »
I'm an old fag. And autodidact, so I have no formal training. I figure everything out for myself.

So. About ten years ago, I was posting on tweakers.net, our largest national tech website, about object oriented programming.

Generally, when there's a new buzzword in computers, people ask me: are you using <fill in newest buzzword>? And I answer, that I don't. They tell me, that I'm old and really should use this, as everyone is doing it. It's the best new thing. When I look it up, there's a lot of noise and marketing. And after some investigation I think: "Ah, that's what they mean. I have been doing that for years. I didn't know that was how it is called."

So, there was this discussion on this website about OO programming, and I joined in. I had done that before it was called like that. Even before Borland Pascal had the Object type. And even in C. And they all told me that I was a moron, didn't understand anything about it, and that wasn't how it worked. For starters, they weren't called objects and didn't use pointers. They were called classes and used methods. And there was no linked list. There were only "object instances".

I tried to explain. And they ganged up on me. They figured I was a stupid, easy target. So I pulled out. I have really wondered multiple times if they were right and I didn't understand.

Anyway, back to C. 40 years ago, C was very basic. C and C++ also have lots of exceptions for many things. But it was popular because it was short and cryptic. It looked like a magic formula, for the uninitiated. Very macho.

The two things C and C++ have inherited form this, are a fascination with speed and that it is really easy to fuck up. It's much harder to do it right. Correctness is not a requirement of the language. But, they did grow up a bit. C got things like members (foo.bar, and even foo->bar). Stricter type checking. Better syntax checking. Lint. Etc. To make it harder to shoot yourself in the foot. Because, 40 years ago, programming in C was really hard to do right.

I just spend a month trying to prevent having to do a large project in C. And you guys want to add the worst things from C to Pascal, simply because C can do that. You want less restrictions, because it allows macho programming. And make it much easier to fuck up. While C and C++ (especially Microsoft and LLVM) try to make C and C++ much, much stricter, to prevent all that. They would rip all that out in an instant, if it wasn't for backward compatibility.

Edit: Interesting video: https://www.youtube.com/watch?v=QM1iUe6IofM
« Last Edit: July 06, 2019, 07:18:09 pm by SymbolicFrank »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #17 on: July 06, 2019, 06:20:47 pm »
Calm don't sir.

I believe many C (and other languages) users here do not mean to make FPC less restricted.

I see thing differently. Many users from other languages found Pascal is great and they want to use it seriously. But they already have written many codes in their previous language. If they can't port their old codes to Pascal, it may become a barrier for them to use Pascal.

I read the thread and saw many suggestions for this issue. Sorry, but it looks ugly to me. I love the simple and clean syntax of Pascal. But think the other side, if Pascal can be easily to port from other languages, it will be a great advantage.

~ Peace ~

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #18 on: July 06, 2019, 07:23:16 pm »
Ever wondered why almost all lessons about "Things you shouldn't do while programming" are about C++? Because most of the things they tell you not to do are C++ specific. You cannot do them wrong in most other programming languages.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #19 on: July 06, 2019, 07:34:53 pm »
Ever wondered why almost all lessons about "Things you shouldn't do while programming" are about C++? Because most of the things they tell you not to do are C++ specific. You cannot do them wrong in most other programming languages.
note: flexible array member feature is not inherited in C++. It's C only feature.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #20 on: July 06, 2019, 07:38:39 pm »
@Jamie,
@Marco,

Thank you for the alternatives you provided/mentioned.


@Handoko

Many users from other languages found Pascal is great and they want to use it seriously. But they already have written many codes in their previous language. If they can't port their old codes to Pascal, it may become a barrier for them to use Pascal.
Exactly!.  Thank you for stating the obvious which seems to be not quite obvious to some.


I read the thread and saw many suggestions for this issue. Sorry, but it looks ugly to me. I love the simple and clean syntax of Pascal.
Pascal's clean syntax is one of its nicest features.  There is usually a clean/Pascal way of implementing a feature found in another language.   A little pondering usually helps get there.  What's important is the desire and dedication to implement it nicely, that's what will make a difference.

But think the other side, if Pascal can be easily to port from other languages, it will be a great advantage.
Not only a great advantage for the Pascal language but, also for those who use it.


@SymbolicFrank

If you really want to program in Pascal like in C, use pointers. Don't rape the language.
Not only is that rather dramatic and over the top, the construction "<vartype> <array[]>" is in C, precisely to be able to obtain a pointer to the desired location within the struct without having to declare a space-consuming field to obtain it.

C and C++ also have lots of exceptions for many things. But it was popular because it was short and cryptic. It looked like a magic formula, for the uninitiated. Very macho.
I believe there is some [added word] truth to that but, the reason some people use C is because, whether cryptically or cleanly, there is usually a way to tell the compiler what you want and the compiler will usually generate decent code for it.  That is a _big_ advantage.  In the case of Windows specifically, the API is spec-ed in C.  That means that, so far, the only way to get access to the full API definition is by using C/C++.  Sometimes just that is enough to use C/C++ instead of Pascal/FPC.  When one is writing a program, the objective is to write the program, not to have to translate function headers and structs from C to Pascal - which in most cases is fairly quick but sometimes the Pascal translation is something that had to be shoe-horned because the language lacks some type description facilities.

The two things C and C++ have inherited form this, are a fascination with speed and that it is really easy to fuck up. It's much harder to do it right.
There is definitely some truth to that but, it's got its pros and cons.  A well optimized program that is snappy is often a pleasure to use (provided it's not riddled with bugs as a result of some ill-conceived optimizations.)

Correctness is not a requirement of the language.
You're right.   Correctness is a requirement of the _programmer_.  That said, it is true that C offers a very limited amount of help in that area.  That's one of the areas they _greatly_ improved in C++.  Type checking in C++ is at least as good as in Pascal.

And you guys want to add the worst things from C to Pascal, simply because C can do that. You want less restrictions, because it allows macho programming. And make it much easier to fuck up.
Not only that isn't the case, it is completely over the top.  Adding simple things like a zero-size field to use as a reference in a struct/record is not unclean in any way.  On the contrary, having that results in code that is much cleaner than the un-intuitive pointer manipulation you have to do when the feature is not available.

The bottom line is this: producing correct programs is the responsibility of the programmer, not the compiler's.  A compiler should provide as many clean constructs as possible to enable the programmer to describe data succinctly and accurately. 

The compiler should help find errors, not get in the way of programming.

While C and C++ (especially Microsoft and LLVM) try to make C and C++ much, much stricter, to prevent all that. They would rip all that out in an instant, if it wasn't for backward compatibility.
They are making it stricter for the compiler to be able to flag potential programming errors but, they are _not_ restricting what the compiler can do.  On the contrary, constructs such as "array[]" are fairly new and they are a much cleaner way  to provide an address to access the field than using "array[ANYSIZE]".


General comment:

From the posts I've read in the forum, I get the impression there are a significant number of Pascal programmers averse to using pointers and variable size structures.  That seems to have been encouraged by the compiler's effort to hide pointer dereferences.  Makes programming "cushy" but, ultimately, it's not a good thing because the "programmer" isn't aware of what is _really_ going on.

A lot of programs can be written without using pointers and/or variable sized structures but, there are programs where _not_ using pointers and/or variable sized structures results in code that is more complicated, usually very wasteful of memory and just as often far from performing as well as they could.  For the user of such a program, that results in an experience that usually leaves something to be desired.


Lastly ...

It would be nice if FPC supported the "array[]" construction.
« Last Edit: July 06, 2019, 07:43:53 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #21 on: July 06, 2019, 07:49:18 pm »
@SymbolicFrank, it is really not about C or C++. There are practical problems that can be solved in elegant way if you know the content of this thread.

The simplest example I can provide maybe the checksum field located at the end of a record of variable length. What's your preferred way to reach that field?

Another example is a DNS packet, it starts with a header like:
Code: Pascal  [Select][+][-]
  1.     TransactionID: Word;
  2.     Flags: Word;
  3.     QuestionsCount: Word;
  4.     AnswersCount: Word;
  5.     AuthorityCount: Word;
  6.     AdditionalCount: Word;

followed by Questions. Each like:
Code: Pascal  [Select][+][-]
  1.     Name: array[0..0] of char;  // sample value: #3www#6google#3com#0
  2.     &Type: Word;
  3.     &Class: Word;

Notice that the field Name is of variable length, how do you reach Type and Class?

Remember you might have more questions in this record. How do you reach other questions? Then it is followed by Answers. Again, each answer is like... etc.

You can not always choose not to have variable length record.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #22 on: July 06, 2019, 08:48:45 pm »
Ok, the argument is: people don't want to change. If we want them to use Pascal, we have to make sure that they can continue programming as they did and reuse all their old code.

Know Lisp? There's an endless amount of things you can do in Lisp, that you cannot do in Pascal. Ok, it's not that popular anymore.

JavaScript. Very popular. Classes are the same as (anonymous) functions, and they're stored as text (or as p-code). Like with .NET WCF. Eval() can do lots of things that are impossible in Pascal.

C and C++ are some of the worst languages, but they're used most. So you should be able to program in Pascal just as you did in C and C++, and reuse all your old code.

Why? If you want to program in C or C++, program in C or C++! Not Hard!

If you want to use a programming language that protects you against 90% of the pitfalls of those languages, while still offering the same possibilities, use Object Pascal.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #23 on: July 06, 2019, 08:52:31 pm »
It would be nice if FPC supported the "array[]" construction.
in what sense?
If you're looking for the option of "Flexible array member", I doubt it's possible from Pascal point of view.
In pascal arrays are always of the known size. (you can always run length() on an array)

In this sort of C-style declaration, the length is never known.

So your best option would be to use a pointer to a pointer to an array.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #24 on: July 06, 2019, 09:10:16 pm »
Btw, My "cool down, level headed" approach:

If you have been there form the start, you know how it all works. Especially if you also did electronics. If I ask a current programmer, who is almost certainly building web apps: "Do you know how the HTTP protocol works?", then, after a while, they admit that they don't. And if they do, I'll ask them "Did you ever build your own HTTP server?". Very few answer "yes" to that question.

That's why SCRUM is popular. Instead of analyzing the project and making a design beforehand, you just pick something simple (most often a bit of GUI) and start building there. Who knows what will happen? It's a journey full of surprises! And that's why current software is very buggy and never works as expected. But it probably looks really cool.

So, imagine if you make those programmers use C++.  :D

Then again, there are a few die-hard, very experienced programmers out there who can program well in any language. And quite a lot of those hang out here. They want more, better and sharper tools. Just for the occasional convenience. But their apprentice will probably do all those things he shouldn't. And it's not hard for a fresh programmer to totally break an existing project.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #25 on: July 06, 2019, 09:16:39 pm »
In pascal arrays are always of the known size. (you can always run length() on an array)
Not really.  In a dynamic array declaration, the dynamic array is a pointer to an array of unknown size.  The size is only known after using SetLength().

In this sort of C-style declaration, the length is never known.
Correct.  The purpose is simply to mark the start of the array.  It's very similar to a dynamic array declaration, the only difference is that in a dynamic array declaration space is reserved to hold a pointer to the array.  In a construct such as "array[]" the compiler would have the address of the array as an offset into the record just as it does for any other field.

Unless I am missing something, I think it would be easy to implement, just one more offset into the symbol table and no storage reserved for the symbol's type.  It would probably be a good idea to restrict such a definition to field records since that's probably the only place where they make sense.


@SymbolicFrank

If we want them to use Pascal, we have to make sure that they can continue programming as they did and reuse all their old code.
No.  (more later)

Know Lisp? There's an endless amount of things you can do in Lisp, that you
cannot do in Pascal. Ok, it's not that popular anymore.

JavaScript. Very popular. Classes are the same as (anonymous) functions, and they're stored as text (or as p-code). Like with .NET WCF. Eval() can do lots of things that are impossible in Pascal.
You're comparing Ducks, Tricycles and Bushels of oranges.  The functional purpose of those languages differ greatly.  Pascal isn't JavaScript and was not designed to do what JavaScript does.

As far as Eval(), if you build in a full Pascal interpreter into your program, it will do everything eval() can do (of course, with the same limitations eval() has in other languages.)


C and C++ are some of the worst languages, but they're used most. So you should be able to program in Pascal just as you did in C and C++, and reuse all your old code.
But the fallacy you've built is that there is no purpose similarity between Pascal and C/C++.  Unlike with JavaScript, there is plenty and, whatever - desirable - features are present in C/C++ could be added to Pascal (often requiring some cleanup in the process.)

Why? If you want to program in C or C++, program in C or C++! Not Hard!
Here is another thing that's not hard.  If you want to program in a better language than C/C++, program in Pascal and slowly add to the Pascal language the useful features found in C/C++ removing the many "warts" in the process.

Eventually, you end up with a well designed language that has every feature needed to program a check book balancing program or a full blown operating system.  That would be very nice.

It seems some people don't like to see features they don't know how to use in a language.


If you want to use a programming language that protects you against 90% of the pitfalls of those languages, while still offering the same possibilities, use Object Pascal.
Good line for a commercial but, if you want to write a capable operating system, use C. 


« Last Edit: July 06, 2019, 09:39:58 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #26 on: July 06, 2019, 10:07:38 pm »
440bx, did you watch the video I linked? I'm pretty sure you'll totally agree. I leave it open if I do  ;)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #27 on: July 06, 2019, 10:17:14 pm »
Not really.  In a dynamic array declaration, the dynamic array is a pointer to an array of unknown size.  The size is only known after using SetLength().
dynamic arrays are managed types. Thus would be set to nil with zero size.

The compiler needs to release dynamic array safely, no matter if SetLength was called or not.

In Pascal the size of array is (must be) always known.
« Last Edit: July 06, 2019, 10:18:58 pm by skalogryz »

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #28 on: July 06, 2019, 10:27:29 pm »
@SymbolicFrank

440bx, did you watch the video I linked? I'm pretty sure you'll totally agree. I leave it open if I do  ;)
What link are you referring to ? ... maybe I missed it but, I don't see a link anywhere. 

Could you please post the link again ?... I will definitely watch it.

@skalogryz

In Pascal the size of array is (must be) always known.
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.  FPC already sees pointers the same way as C, as the address of an array.

That statement that in Pascal the size of an array is/"must be" always known isn't really true.  The way FPC treats/uses pointers breaks that rule.  It basically already sees typed pointers as an array[] declaration.


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

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313

 

TinyPortal © 2005-2018