Forum > General

Getting const offset of nested record field

(1/2) > >>

scribly:
I'm trying to get a const filled with the offset of a field in a record.  I can get it to work for top fields, but not for nested fields


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TSomething=record    a: integer;    b: integer;    c: integer;    d: record      a: integer;      b: integer;      c: integer;      d: integer;      e: integer;    end;    e: integer;  end;   PSomething=^TSomething; const  works=PtrUInt(@PSomething(nil)^.b);  worksaswell=PtrUInt(@PSomething(nil)^.d);  doesnotwork=PtrUInt(@PSomething(nil)^.d.c); var doeswork: ptruint; procedure initDoesWork();begin  doeswork:=PtrUInt(@PSomething(nil)^.d.c);end; 
I can of course use runtime code to fill it in, but it would be nice if there's a way to fill this in at compiletime
Any suggestions or there's really  no other way ?

Nicole:
you mix pointer and record.
The pointer points to the field, but then the address has done its job.
IMHO pointers cannot nest, because they are pointers.

Is it an option for you, just to fill in the integers as numbers or would this destroy your plan?

PascalDragon:

--- Quote from: scribly on August 02, 2022, 04:40:53 pm ---I can of course use runtime code to fill it in, but it would be nice if there's a way to fill this in at compiletime
--- End quote ---


If you declare your function as inline it should be rather similar to a constant so you could use that as a workaround.


--- Quote from: scribly on August 02, 2022, 04:40:53 pm ---Any suggestions or there's really  no other way ?

--- End quote ---

Would you please test with FPC 3.3.1, cause there might have been a few changes recently. Otherwise please report a bug.


--- Quote from: Nicole on August 02, 2022, 06:14:14 pm ---you mix pointer and record.
The pointer points to the field, but then the address has done its job.
IMHO pointers cannot nest, because they are pointers.

--- End quote ---

You can perfectly fine retrieve pointers to records, its fields or fields of nested records no matter if at compile- or runtime.

MarkMLl:

--- Quote from: scribly on August 02, 2022, 04:40:53 pm ---I'm trying to get a const filled with the offset of a field in a record.  I can get it to work for top fields, but not for nested fields

--- End quote ---

Silly question, but are things being made worse by the fact that the main and subfields have a common name?

I'd suggest that you might do better if you broke the nested field out as its own type, computed the offsets of its fields separately, and then added those to the main field offset as appropriate.

That would leave the problem of variant records, which (with care) would probably be amenable to the same treatment, otherwise obviously note PascalDragon's comments.

In all cases, I'd be inclined to have assertions somewhere to make sure that integer (etc.) actually does have the size expected. I'd also be inclined to put a comment wherever the non-obvious @PSomething(nil)^ construct is used, so that anybody reading the code understands that it's simulating a variable at address zero... and I don't know whether this is necessary but it might be worth having an explicit check that PtrUInt(nil) is zero since while I don't think this applies to FPC I've seen platforms where it didn't hold.

MarkMLl

bytebites:
Compile without errorrs with 3.3.1. (Just add begin end. to the end of the program.

Navigation

[0] Message Index

[#] Next page

Go to full version