Recent

Author Topic: A suggestion for a new FPC feature  (Read 566 times)

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 494
Re: A suggestion for a new FPC feature
« Reply #15 on: June 24, 2019, 05:09:13 pm »
Code: Pascal  [Select]
  1. typedef struct {
  2.   GUID  PowerSetting;
  3.   DWORD DataLength;
  4.   UCHAR Data[1];
  5. } POWERBROADCAST_SETTING, *PPOWERBROADCAST_SETTING;

It stores a pointer to Data.
No.  It doesn't store a pointer.  It stores the array of UCHAR directly in the structure.  The compiler doesn't know that there is (likely) more than 1 character stored in the last field which is the reason for the "DataLength" field.  That's the field that allows the programmer to use the correct array size at runtime.

The structure size varies but, the compiler sees it as a fixed size structure because it is being "lied" to by specifying [1] as the number of array elements.

Interesting. That does sound very Microsoft. I would strongly prefer not to have something like that in fpc.

lucamar

  • Hero Member
  • *****
  • Posts: 1608
Re: A suggestion for a new FPC feature
« Reply #16 on: June 24, 2019, 07:25:30 pm »
Interesting. That does sound very Microsoft. I would strongly prefer not to have something like that in fpc.

But you do have something like that in Pascal:
Code: Pascal  [Select]
  1. TVariableRecord = record
  2.   Size: Integer;
  3.   Data: array [0..0] of byte;
  4. end;
:o
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 494
Re: A suggestion for a new FPC feature
« Reply #17 on: June 24, 2019, 07:41:00 pm »
And how do I put data in Data? SetLength doesn't work.

I don't really believe it.

440bx

  • Hero Member
  • *****
  • Posts: 838
Re: A suggestion for a new FPC feature
« Reply #18 on: June 24, 2019, 07:50:21 pm »
Interesting. That does sound very Microsoft. I would strongly prefer not to have something like that in fpc.
Microsoft isn't the only one who has used that "feature".  It's not what could be characterized as common but, it isn't really rare either.   

It's a lot of "fun" when not only the field size varies but the semantics of the field - including its data type - vary depending on the value of some other variable in the structure/record.   Basically a record whose variant is "squeezed" to fit each type.

It's more something of a C programmer's thing than a Microsoft thing.

Programmers always find ways to abuse a feature. <chuckle>  I suspect that's what's made the C language popular.

Going back to the topic of the thread, removing the restriction that a variant must be the last field in a record and allowing multiple variants in a record would really ease porting C data structures to Pascal.  We live in a C world whether we like it or not.

ETA:

And how do I put data in Data? SetLength doesn't work.

I don't really believe it.
you load data in that field the same way you load data into an array (the field is an array), you're simply not respecting the array bounds (whatever they may be) and, since you are ignoring the declared array bounds, you need to either manually, set the count of elements or the size in bytes, into some variable dedicated to hold it for the structure to be usable.

Believe or not... and it's not Ripley's, it's Pascal (and C, C++ and quite a few other languages that implement that "trick".)
« Last Edit: June 24, 2019, 07:58:25 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 494
Re: A suggestion for a new FPC feature
« Reply #19 on: June 24, 2019, 07:58:10 pm »
So, you malloc() a buffer, memcpy() the record into it, and then manage everything yourself? Like increasing the pointer to the right location?

You don't need the record for that. It has no purpose.

Edit: and make sure to turn off any range or bounds checking. Yes, C. Not Pascal.
« Last Edit: June 24, 2019, 08:00:45 pm by SymbolicFrank »

440bx

  • Hero Member
  • *****
  • Posts: 838
Re: A suggestion for a new FPC feature
« Reply #20 on: June 24, 2019, 08:09:02 pm »
So, you malloc() a buffer, memcpy() the record into it, and then manage everything yourself? Like increasing the pointer to the right location?

You don't need the record for that. It has no purpose.
You're getting the idea... it is almost always used with dynamically allocated memory.  It definitely has a purpose, when the size of the array can vary a significant amount and the program needs to keep track of many such records, it is very useful.

For instance, if you write a text editor with an "undo" feature (good feature to have in a text editor), you can have the fixed part of the record describe the memory block it applies to, its sequence and whatever should be put back when the "undo" function is carried out (that part obviously varies in size.) Having every potential undo action neatly packaged in its own variable length record is a much better solution than keeping a pointer to the undo data in the record (the reason is because, using a pointer to another block means there could be a very significant amount of fragmentation.)

Anytime you want to lessen memory fragmentation, records of that kind are good candidates.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 494
Re: A suggestion for a new FPC feature
« Reply #21 on: June 24, 2019, 08:28:36 pm »
If you want to program like that, I would suggest using assembler or C.

lucamar

  • Hero Member
  • *****
  • Posts: 1608
Re: A suggestion for a new FPC feature
« Reply #22 on: June 24, 2019, 08:55:48 pm »
If you want to program like that, I would suggest using assembler or C.

It is not usually a question of "want" but of "need". As a Pascal programmer I love having everytihng as neat and tidy and elegant as possible. But sometimes the real world  insists on intruding with these frankly outrageous demands and, isn't it nice that if you need it you have this kind of features available? :)
« Last Edit: June 24, 2019, 09:00:45 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 494
Re: A suggestion for a new FPC feature
« Reply #23 on: Today at 01:43:29 am »
No. I don't agree. There are always many good ways to do something.

Every project I do is custom and completely different from all my previous ones. I'll adjust and use what the others that cannot have to use.

It's not that I didn't try other things than Object Pascal, most of the time I use something else. I like Object Pascal because it is the most dependable. It won't try to get me to shoot in my own foot. It still works 10 years later.

If you're young, you try to outsmart the others by coming up with border cases that are hard to understand, but work. If you become older, you want things to be understandable.

And not in the first place for me. I'm smart enough to figure it out. But for everyone else.