SizeInt: max value? lazarus / 21-General ---------------------------------------------------------------------- MarkMLl · 2023-11-14 09:09 At https://www.freepascal.org/docs-html/current/rtl/system/pos.html Pos() is documented as returning a SizeInt. At https://www.freepascal.org/docs- html/current/rtl/system/sizeint.html SizeInt is shown as being equivalent to a LongInt, but is documented as having a size which depends on platform. That is inconsistent. At https://www.freepascal.org/docs- html/current/rtl/system/longint.html Longint is shown as having a maximum value of 2147483647, which appears to match the numeric value of MaxLongInt documented at https://www.freepascal.org/docs- html/current/rtl/system/maxlongint.html What is the symbolic name of the maximum value of SizeInt, and where is it documented? MarkMLl ---------------------------------------------------------------------- Jorg3000 · 2023-11-14 10:42 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type SizeInt = LongInt;... is wrong, or is only true for 32-bit systems. It's wrong on this page: https://www.freepascal.org/docs- html/current/rtl/system/sizeint.html But the description for SizeInt is correct: "... thus it is 64-bit on 64-bit platforms, 32-bit on 32-bit platforms, and 16 bit on 16 bit platforms." The description of SizeInt should also refer to Int64  https://www.freepascal.org/docs-html/current/rtl/system/int64.html ---------------------------------------------------------------------- marcov · 2023-11-14 11:07 Afaik sizeint is basically size_t, and must be able to hold the difference between any two pointers. But segmented models might complicate that, because offsets in code in such models are within one segment (for non HUGE models), while pointers are 16-bit segmented, so 32-bit in size, and 20-bit in address space. ---------------------------------------------------------------------- MarkMLl · 2023-11-14 11:16 Quote from: marcov on November 14, 2023, 12:07:32 pmAfaik sizeint is basically size_t, and must be able to hold the difference between any two pointers. But segmented models might complicate that, because offsets in code in such models are within one segment (for non HUGE models), while pointers are 16-bit segmented, so 32-bit in size, and 20-bit in address space. Yes. So where's a symbolic name for the maximum value defined? Or something which, by convention, can stand in for the maximum value? size_t is defined- or at least documented FWIW, as cuint64. But if I assumed the maximum possible value of that, it would probably be incompatible with e.g. System.Copy() on 32-bit systems since it expects its index and count to be SizeInt. This all came about since I wanted to define Poz() as a quick hack returning some implausibly large number rather than zero if the substring was not found. MarkMLl ---------------------------------------------------------------------- marcov · 2023-11-14 11:33 Quote from: MarkMLl on November 14, 2023, 12:16:23 pmQuote from: marcov on November 14, 2023, 12:07:32 pmAfaik sizeint is basically size_t, and must be able to hold the difference between any two pointers. But segmented models might complicate that, because offsets in code in such models are within one segment (for non HUGE models), while pointers are 16-bit segmented, so 32-bit in size, and 20-bit in address space. Yes. So where's a symbolic name for the maximum value defined? Or something which, by convention, can stand in for the maximum value? Afaik there is none, also the highest value is not necessarily valid. E.g. in 64-bit targets, the maximum sizeint would be more than the virtual space of the system. (which is usually 48-52 bit) ---------------------------------------------------------------------- MarkMLl · 2023-11-14 11:43 Quote from: marcov on November 14, 2023, 12:33:27 pmAfaik there is none, also the highest value is not necessarily valid. E.g. in 64-bit targets, the maximum sizeint would be more than the virtual space of the system. (which is usually 48-52 bit) This year. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2023-11-16 20:55 Quote from: MarkMLl on November 14, 2023, 10:09:42 amAt https://www.freepascal.org/docs-html/current/rtl/system/pos.html Pos() is documented as returning a SizeInt. At https://www.freepascal.org/docs- html/current/rtl/system/sizeint.html SizeInt is shown as being equivalent to a LongInt, but is documented as having a size which depends on platform. That is inconsistent. The documentation is generated for a specific platform (i386-linux), thus the documentation matches that. Quote from: MarkMLl on November 14, 2023, 10:09:42 amWhat is the symbolic name of the maximum value of SizeInt, and where is it documented? There is none. The maximum value of SizeInt can be determined by High(SizeInt). Quote from: marcov on November 14, 2023, 12:33:27 pmAfaik there is none, also the highest value is not necessarily valid. E.g. in 64-bit targets, the maximum sizeint would be more than the virtual space of the system. (which is usually 48-52 bit) The value $FFFFFFFF_FFFFFFFF is a valid address, because the invalid addresses are in the middle of the address space, splitting the address space into two equally sized halfs. Usually operating systems put the priviledged memory into the upper range and the user addressable one in the lower one, though that's no hard requirement. ---------------------------------------------------------------------- marcov · 2023-11-16 21:42 QuoteThe value $FFFFFFFF_FFFFFFFF is a valid address, because the invalid addresses are in the middle of the address space, splitting the address space into two equally sized halfs. Usually operating systems put the priviledged memory into the upper range and the user addressable one in the lower one, though that's no hard requirement. I know that, but there shouldn't be a reason to do a pointer subtraction between user and kernel space. ---------------------------------------------------------------------- MarkMLl · 2023-11-17 07:34 Quote from: PascalDragon on November 16, 2023, 09:55:23 pmThe documentation is generated for a specific platform (i386-linux), thus the documentation matches that. In that case it should be described as such. QuoteThere is none. The maximum value of SizeInt can be determined by High(SizeInt). Just the answer I was looking for: I always overlook High() as being applicable to scalars. Somebody should have pointed out my silly omission earlier :-) MarkMLl ---------------------------------------------------------------------- MarkMLl · 2023-11-17 07:37 Quote from: marcov on November 16, 2023, 10:42:23 pmQuoteThe value $FFFFFFFF_FFFFFFFF is a valid address, because the invalid addresses are in the middle of the address space, splitting the address space into two equally sized halfs. Usually operating systems put the priviledged memory into the upper range and the user addressable one in the lower one, though that's no hard requirement. I know that, but there shouldn't be a reason to do a pointer subtraction between user and kernel space. I'd suggest that's not a valid excuse. Pascal doesn't have "types with holes", and subtraction is a valid operation. MarkMLl ---------------------------------------------------------------------- Zoran · 2023-11-17 16:30 Quote from: MarkMLl on November 14, 2023, 10:09:42 amWhat is the symbolic name of the maximum value of SizeInt, and where is it documented? Such constants are not needed in modern Pascal. Use type helpers from SysUtils: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} // This does not matter. Works in any mode. uses  SysUtils; var  SMax, SMin: SizeInt; begin  SMax := SizeInt.MaxValue; // you can reference from type directly  SMin := SMin.MinValue; // or reference from the variable itself   WriteLn('Max SizeInt: ', SMax);  WriteLn('Min SizeInt: ', SMin);   readln;end. ---------------------------------------------------------------------- MarkMLl · 2023-11-18 07:57 Quote from: Zoran on November 17, 2023, 05:30:04 pmQuote from: MarkMLl on November 14, 2023, 10:09:42 amWhat is the symbolic name of the maximum value of SizeInt, and where is it documented? Such constants are not needed in modern Pascal. Use type helpers from SysUtils: Where's that documented? MarkMLl ---------------------------------------------------------------------- ASerge · 2023-11-18 08:08 Quote from: MarkMLl on November 18, 2023, 08:57:59 amWhere's that documented? https://www.freepascal.org/docs- html/current/rtl/sysutils/tint64helper.html ---------------------------------------------------------------------- cdbc · 2023-11-18 08:49 Hi edit: ASerge was faster... The ".MaxValue" and ".MinValue" are consts of TInt64Helper. Regards Benny ---------------------------------------------------------------------- MarkMLl · 2023-11-18 09:12 Quote from: cdbc on November 18, 2023, 09:49:39 amHi edit: ASerge was faster... The ".MaxValue" and ".MinValue" are consts of TInt64Helper. So how does that help a SizeInt, particularly on a platform where it's not 64-bit? MarkMLl ---------------------------------------------------------------------- Zoran · 2023-11-18 10:18 Quote from: MarkMLl on November 18, 2023, 10:12:21 amQuote from: cdbc on November 18, 2023, 09:49:39 amHi edit: ASerge was faster... The ".MaxValue" and ".MinValue" are consts of TInt64Helper. So how does that help a SizeInt, particularly on a platform where it's not 64-bit? MarkMLl Then you will get results from TIntegerHelper. SizeInt does not need its own helper, as it is always just an alias for another type. ---------------------------------------------------------------------- Thaddy · 2023-11-18 15:46 That is.. as long as you you realize integer helpers just are valid for half the actual address space... ---------------------------------------------------------------------- MarkMLl · 2023-11-18 19:53 Quote from: Zoran on November 18, 2023, 11:18:01 amThen you will get results from TIntegerHelper. SizeInt does not need its own helper, as it is always just an alias for another type. Thanks, that's useful. But I still feel there's issues about how to get to this stuff via the documentation :-/ MarkMLl ---------------------------------------------------------------------- MarkMLl · 2023-11-18 20:52 Quote from: Thaddy on November 18, 2023, 04:46:44 pmThat is.. as long as you you realize integer helpers just are valid for half the actual address space... Thaddy, could you expand on what you mean there please? I'm uncomfortable with this business for lots of reasons: difficulty finding the information via the documentation, no clear indication of when helpers were added and so on. MarkMLl ---------------------------------------------------------------------- Zoran · 2023-11-19 11:04 Quote from: MarkMLl on November 18, 2023, 09:52:24 pmQuote from: Thaddy on November 18, 2023, 04:46:44 pmThat is.. as long as you you realize integer helpers just are valid for half the actual address space... Thaddy, could you expand on what you mean there please? I'm uncomfortable with this business for lots of reasons: difficulty finding the information via the documentation, no clear indication of when helpers were added and so on. MarkMLl What he means is that, in case you actually use SizeInt for addressing, you should be aware that a signed type can cover only the half of the possible address space (the other half is seen as negative numbers). So, for that purpose SizeUInt would be better choice (and it will use the corresponding helper of the unsigned type - see TCardinalHelper, TQWordHelper). There are also PtrInt, PtrUInt, NativeInt, NativeUInt ---------------------------------------------------------------------- MarkMLl · 2023-11-19 11:15 Quote from: Zoran on November 19, 2023, 12:04:22 pmWhat he means is that, in case you actually use SizeInt for addressing, you should be aware that a signed type can cover only the half of the possible address space (the other half is seen as negative numbers). So, for that purpose SizeUInt would be better choice (and it will use the corresponding helper of the unsigned type - see TCardinalHelper, TQWordHelper). OK, but my OP specifically asks about Pos() which returns a SizeInt. MarkMLl ---------------------------------------------------------------------- Zoran · 2023-11-19 11:23 Quote from: MarkMLl on November 19, 2023, 12:15:20 pmQuote from: Zoran on November 19, 2023, 12:04:22 pmWhat he means is that, in case you actually use SizeInt for addressing, you should be aware that a signed type can cover only the half of the possible address space (the other half is seen as negative numbers). So, for that purpose SizeUInt would be better choice (and it will use the corresponding helper of the unsigned type - see TCardinalHelper, TQWordHelper). OK, but my OP specifically asks about Pos() which returns a SizeInt. MarkMLl It has nothing to do with Thaddy's comment. You are safe there. ---------------------------------------------------------------------- Zoran · 2023-11-19 11:36 Quote from: Zoran on November 19, 2023, 12:23:33 pmQuote from: MarkMLl on November 19, 2023, 12:15:20 pm OK, but my OP specifically asks about Pos() which returns a SizeInt. MarkMLl It has nothing to do with Thaddy's comment. You are safe there. Well, that is unless your string is longer than SizeInt.MaxValue and the first occurrence of the searched substring is found beyond that.  But note that Length() function also returns SizeInt, so I guess an AnsiString cannot be longer than that. ---------------------------------------------------------------------- MarkMLl · 2023-11-19 11:39 Quote from: Zoran on November 19, 2023, 12:23:33 pmIt has nothing to do with Thaddy's comment. You are safe there. I'm sure I'm not the only one who Thaddy's comments make feel unsafe :-) Incidentally, I came across an interesting book about 50 years ago which focused on linkers and loaders: in fact that was probably its title. I think it was oriented towards (IBM) mainframes where those are separate operations, but it drew parallels between addresses/sizes and vectors/scalars and the range of operations which could be performed on them. Now considering Marco's comment that SizeInt is defined by the size of a pointer on any given architecture, I think it's problematic that if Code: [Select]@A <> @B then Code: [Select](@A - @B) <> (@B - @A) which has the implication that if the pointer length is N bits then the size length must be N+1 bits to accommodate the fact that subtraction of two addresses yields a signed result. So even allowing that chunks of the x86 address space are reserved for memory-mapped peripherals, a system which potentially had 64-bit pointers needs a 65-bit type to represent the difference between two addresses. MarkMLl ---------------------------------------------------------------------- Zoran · 2023-11-19 11:49 Marc, that is beyond me. Anyway, check if you see my previous post (before you posted, I added another comment about why I think you are safe with Pos). Quote from: MarkMLl on November 19, 2023, 12:39:19 pmNow considering Marco's comment that SizeInt is defined by the size of a pointer on any given architecture, No, unlike PtrInt, SizeInt is not always the size of the pointer - see this: https://forum.lazarus.freepascal.org/index.php/topic,33269.0.html ---------------------------------------------------------------------- MarkMLl · 2023-11-19 11:53 Quote from: Zoran on November 19, 2023, 12:36:50 pmBut note that Length() function also returns SizeInt, so I guess an AnsiString cannot be longer than that. I think I also noticed that some of the primitive pointer operations come in two forms depending on whether SizeInt is the same size as Integer. My particular focus here is Copy(), where it would be desirable to have a literal constant which was unambiguously beyond the end of any possible string. Hence if e.g. Poz(...) didn't find a match then Copy(1, Poz(...)) would return the original string rather than a blank string. MarkMLl ---------------------------------------------------------------------- Zoran · 2023-11-19 12:03 Quote from: MarkMLl on November 19, 2023, 12:53:34 pmQuote from: Zoran on November 19, 2023, 12:36:50 pmBut note that Length() function also returns SizeInt, so I guess an AnsiString cannot be longer than that. I think I also noticed that some of the primitive pointer operations come in two forms depending on whether SizeInt is the same size as Integer. My particular focus here is Copy(), where it would be desirable to have a literal constant which was unambiguously beyond the end of any possible string. Hence if e.g. Poz(...) didn't find a match then Copy(1, Poz(...)) would return the original string rather than a blank string. MarkMLl So, if Lenght of the string cannot be more than SizeInt.MaxValue, you are safe with SizeInt.MaxValue. Btw, did you know that you can omit the length parameter in Copy? That is - Copy(S, 3) returns substring from third character to the end of the string, same as Copy(S, 3, Length(S)), and same as Copy(S, 3, SizeInt.MaxValue). ---------------------------------------------------------------------- Zoran · 2023-11-19 12:18 Quote from: MarkMLl on November 19, 2023, 12:53:34 pm Hence if e.g. Poz(...) didn't find a match then Copy(1, Poz(...)) would return the original string rather than a blank string. If I understood well, you can just use something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CopyToPos(const SubStr: String; const Str: String): String;var  Sz: SizeInt;begin  Sz := Pos(SubStr, Str);  if Sz = 0 then    Exit(Str);   Result := Copy(Str, 1, Sz - 1);end; ---------------------------------------------------------------------- MarkMLl · 2023-11-19 12:48 Quote from: Zoran on November 19, 2023, 01:18:21 pmIf I understood well, you can just use something like this: Yes, but sooner or later one forgets the check (or at least I did :-) and has a tedious bit of debugging to do. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2023-11-19 16:51 Quote from: MarkMLl on November 17, 2023, 08:34:25 amQuote from: PascalDragon on November 16, 2023, 09:55:23 pmThe documentation is generated for a specific platform (i386-linux), thus the documentation matches that. In that case it should be described as such. It should be mentioned in the preface of the documentations for what platform they have been generated, cause that description applies to many types, functions and even units (e.g. units or functions not existing on Windows). Feel free to file a bug report against the documentation to have that improved. Quote from: MarkMLl on November 17, 2023, 08:34:25 amQuoteThere is none. The maximum value of SizeInt can be determined by High(SizeInt). Just the answer I was looking for: I always overlook High() as being applicable to scalars. Somebody should have pointed out my silly omission earlier :-) I did as early as possible. ---------------------------------------------------------------------- MarkMLl · 2023-11-19 17:51 Quote from: PascalDragon on November 19, 2023, 05:51:32 pmFeel free to file a bug report against the documentation to have that improved. I would, but the last time I looked Gitlabs appeared to have thrown me out. Are lightly-used accounts deleted? QuoteI did as early as possible.  :-) MarkMLl ---------------------------------------------------------------------- PascalDragon · 2023-11-19 17:53 Quote from: MarkMLl on November 19, 2023, 06:51:06 pmQuote from: PascalDragon on November 19, 2023, 05:51:32 pmFeel free to file a bug report against the documentation to have that improved. I would, but the last time I looked Gitlabs appeared to have thrown me out. Are lightly-used accounts deleted? I don't know, that's not managed by us, but by GitLab itself 🤷‍♀️ ---------------------------------------------------------------------- MarkMLl · 2023-11-20 08:09 Quote from: PascalDragon on November 19, 2023, 06:53:17 pmQuote from: MarkMLl on November 19, 2023, 06:51:06 pmQuote from: PascalDragon on November 19, 2023, 05:51:32 pmFeel free to file a bug report against the documentation to have that improved. I would, but the last time I looked Gitlabs appeared to have thrown me out. Are lightly-used accounts deleted? I don't know, that's not managed by us, but by GitLab itself 🤷‍♀️ Yes, I know. Wasn't blaming you for an instant. MarkMLl