Recent

Author Topic: SScanf in RTL states it can handle only Extended, Doc error or true?  (Read 415 times)

jamie

  • Hero Member
  • *****
  • Posts: 7707
I am using the "SScanF" function to scan a string of 'x,x' in float format but the docs state it only supports Extended for the '%f' switch.

This is all well and good, but I am having what appears to be stack corruption issues or overflows of destination targets That hold Doubles.

I locally have 2 Extended on the stack, read in a string and parse it via the SSCanF(str,'$f,%f',[ @Y, @X ]);

The "X,Y" are on the local stack. I store there and then write those values to a double on the outgoing of the function.

I know in 64bit mode Extended are translated to Double but is the DOC wrong about the SSCANF or is it really storing 10 bytes on targets that are actually 8 bytes?

Jamie


The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: SScanf in RTL states it can handle only Extended, Doc error or true?
« Reply #1 on: April 26, 2026, 04:24:04 pm »
10 on 32,  8 on 64: sscanf uses the platform defaults which is double on 64 bit windows. Which is documented. Only the doc assumes you know that the platform is extended = double.
A pascal programmer would use read/readln.
« Last Edit: April 26, 2026, 04:42:50 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: SScanf in RTL states it can handle only Extended, Doc error or true?
« Reply #2 on: April 26, 2026, 04:36:10 pm »
Well, that's nice that you have documentation in front of you that states that, meanwhile I am looking at it stating that it's Extended.

There is no mention about platform differences and stating that fact, but I know there is!

https://www.freepascal.org/docs-html/rtl/sysutils/sscanf.html

I don't see it.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: SScanf in RTL states it can handle only Extended, Doc error or true?
« Reply #3 on: April 26, 2026, 04:50:16 pm »
Extended is double on 64 bit Windows. And that is documented.
The Windows64 ABI does not support extended 10 bytes.
You looked in the wrong place. Not FPC, but Windows docs.

On this case, the wiki is correct.
"Note that for Windows 64 bits and non-Intel targets Extended is an alias for Double."

On Linux64 it is default 10, not 8.
« Last Edit: April 27, 2026, 07:18:01 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6395
  • Compiler Developer
Re: SScanf in RTL states it can handle only Extended, Doc error or true?
« Reply #4 on: April 27, 2026, 09:52:49 pm »
I know in 64bit mode Extended are translated to Double but is the DOC wrong about the SSCANF or is it really storing 10 bytes on targets that are actually 8 bytes?

sscanf() internally uses PExtended, so it's best to use Extended as variable type to avoid potential issues across platforms. If Extended is Double (namely on x86_64-win64 and all non-x86 platforms) then this will work transparently.

LeP

  • Sr. Member
  • ****
  • Posts: 304
Re: SScanf in RTL states it can handle only Extended, Doc error or true?
« Reply #5 on: April 27, 2026, 10:07:17 pm »
Report from: https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Extended

Quote
On Intel 32-bit Windows systems, the size of System.Extended is 10 bytes.
On Intel 32-bit macOS or iOS-Simulator systems, the size of System.Extended is 16 bytes in order to be compatible with BCCOSX.
On Intel Linux systems, the size of System.Extended is 16 bytes.
On Intel 64-bit Windows systems, however, the System.Extended type is an alias for System.Double, which is only 8 bytes. This difference can adversely affect numeric precision in floating-point operations. For more information, see Delphi Considerations for Multi-Device Applications.
On ARM system include iOS and Android, however, the System.Extended type is an alias for System.Double, which is only 8 bytes. This difference can adversely affect numeric precision in floating-point operations.
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

 

TinyPortal © 2005-2018